How to Exclude Certain Columns in EF6 Using Database First? [duplicate]

删除回忆录丶 提交于 2020-01-17 06:02:12

问题


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?

来源:https://stackoverflow.com/questions/38445897/how-to-exclude-certain-columns-in-ef6-using-database-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!