2

I have auto generated model from a database in Entity Framework version 4.1.10331.0.

I want to ignore a column from an entity without using the Fluent Api and without changing the ObjectContext into DbContext (and of course without deleting the column from the SQL table) and without marking the property generated in the model with the attribute NotMapped, because whenever I update my context in the model that column will reappear.

Can someone please help me in this case?

Thanks and best regards Ben

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adrian10 BEN
  • 1,283
  • 2
  • 11
  • 15
  • If you want to ignore, ignore it. What does this have to do with programming? Do you mean not fetching data for that column from database or what? – Mert Akcakaya May 10 '13 at 06:28
  • yes mert i mean not to featching the data for that column – Adrian10 BEN May 10 '13 at 06:30
  • oops. I didnt notice that comment. You mean, just not fetch the data? Or do you mean to not include the column in the generated class definition at all (so the "myShoeSize" is never ever seen anywhere in the code and it is "secret column" that exists only in the DB?). – quetzalcoatl May 10 '13 at 06:44

2 Answers2

4

I don't see the problem updating your EF each time you regenerate the model, but I can propose 2 solutions:

  1. Create a View that contains the columns you need, then generated it in EF.
  2. Create another class derived from you entity that will show the data you want. This class will be your "application Entity" (As you know additional management should be considered here)
noobob
  • 532
  • 4
  • 12
0

EF database-first is very under-tooled in many places. Similarily to your problem, if you generate a model from DB and rename a column in CodeSpace (so column users.col_chr_UsrName is just User.Name), you also would lose it when regenerating the model.

If I remember well, in EF3, EF4 and even in EF5 there is no way to preserve them. If you just "update" the model, they have a chance of surviving, but regenerating never preserves anything.

You can try to create a script or set of scripts that you will run after regenerating, and those scripts may seek and apply fixes to the generated model. But thats, well, "workaround" (literally, work and around), not a real solution.

Another thing, with more work, is to define Views or StoredProcedures (or custom table mappings) that will handle the projection, but they sometimes also may get hairy after regenerating (especially custom table maping which will always evaporate).

You can actually ignore the unwanted columns and prepare a set of light LINQ wrappers/accessors that will perform the projection, and put them in some static MyTables class and use that class instead of RawTable. That will work and may be usable, but is not again pretty.

IMHO, the best approach is to use either a script that will fix the model afterwards, or live with the unwanted columns, or .. not use the autogeneration from within the designer. Try to find another, more smart, generator.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107