在实体框架4.1中排除列可更新代码 [英] Exclude column from being updateable in Entity Framework 4.1 Code First
问题描述
如果您使用附件实体EF将仅为已更改的字段生成更新。如果你正在使用分离的实体,你必须手动说EF改变了什么。如果你这样称呼:
context.Entry(yourEntity).State = EntityState.Modified;
您正在说EF,所有属性都应通过修改。但是如果你这样称呼:
context.Entry(youreEntity).Property(e => e.SomeProperty).IsModified = true;
你会说只有 SomeProperty
被修改(只有这个属性将被更新)。我不知道您是否可以通过将整个实体标记为已修改并选择不应修改的属性来进行相反的操作,但您可以自己测试。
如果您的 CreatedOn
填写在数据库中,您可以将其标记为 DatabaseGeneratedOption.Identity
,并且将永远不会被您的应用程序修改。
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?
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.
这篇关于在实体框架4.1中排除列可更新代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!