Exclude column from being updateable in Entity Framework 4.1 Code First

不羁岁月 提交于 2019-12-06 22:26:20

问题


Does anyone know if we can exclude column from being updated in Entity Framework 4.1 Code First? For example I have 'CreatedOn' field that I don't want to included when doing edit/updates. Is this possible, i.e. selectively excluding field from update operation in EF Code First 4.1?


回答1:


If you are working with attached entities you EF will generate updates only for fields which have changed. If you are working with detached entities you must manually say EF what has changed. If you call this:

context.Entry(yourEntity).State = EntityState.Modified;

you are saying EF that all properties should by modified. But if you instead call this:

context.Entry(youreEntity).Property(e => e.SomeProperty).IsModified = true;

you will say that only SomeProperty is modified (only this property will be in update). I'm not sure if you can do the reverse operation by marking the whole entity as modified and select properties which should not be modified but you can test it yourselves.

If your CreatedOn is filled in the database you can mark it as DatabaseGeneratedOption.Identity and it will be never modified by your application.



来源:https://stackoverflow.com/questions/6115406/exclude-column-from-being-updateable-in-entity-framework-4-1-code-first

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