How to Exclude Certain Columns in EF6 Using Databa

2019-09-13 20:43发布

问题:

This question already has an answer here:

  • Entity Framework: Ignore Columns 6 answers
  • How do you update an edmx file with database changes? 6 answers

I am working on a project which has a fairly complex database, and the code is stop-gap while another system overhaul is underway.

I am fixing up some legacy code, and need to perform work quickly and use work-arounds wherever possible.

There is an existing database and I am using EF6 Datbase First with much success.

However, I am discovering various cases (up to my 4th one now) where a particular column is not needed in my code, and EF is tripping up on type conversion issues.

I have found the fast solution (and preferred by my client) is to simply remove these columns from the EF model - which I have been doing manually after "update from database" - which I must do each time I fix a view or table, etc.

Is there a way I can somehow instruct EF "Update from Datbase" operation to ignore certain columns?

I have seen this for Code First - where OnModelCreating can be overridden to set an ignore property - or where a similar attribute is added to the existing property to inform EF when to ignore certain properties.

I had thought about using a partial class - defined in a .cs file in a subfolder where I could put some code to cause this to happen - however I can not annotate the property - as it would be defined twice - once in my partial, and one in the .cs generated by the T4 template.

I've been searching online for a technique to manage this, and I have not found anything.

For example:

Database has Table X, Column1, Column2, Column3.

Update from Database will create entity model for Table X, with all 3 columns.

After Update From Database, I can removed "Column2" reference, and all is well - there is no Column2 property on the model class, and my code simply never references any data in Column2.

What I am seeking is something I can put somewhere - in the database, the EF model, or partial class that will not be over-written each time I do an Update from Database.

Any suggestions?