在 Entity Framework 4.1 Code First 中排除列可更新
有谁知道我们是否可以在 Entity Framework 4.1 Code First 中排除列的更新?例如,我有“CreatedOn”字段,我不想在进行编辑/更新时包含该字段。这是否可能,即有选择地从 EF Code First 4.1 中的更新操作中排除字段?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在使用附加实体,则 EF 将仅为已更改的字段生成更新。如果您正在使用分离的实体,则必须手动告诉 EF 发生了什么变化。如果您这样称呼:
您是在说 EF 所有属性都应该修改。但如果您改为这样调用:
您会说只有
SomeProperty
被修改(只有此属性将被更新)。我不确定您是否可以通过将整个实体标记为已修改并选择不应修改的属性来执行反向操作,但您可以自己测试。如果您的
CreatedOn
已填充到数据库中,您可以将其标记为DatabaseGeneeratedOption.Identity
,并且您的应用程序永远不会修改它。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:
you are saying EF that all properties should by modified. But if you instead call this:
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 asDatabaseGeneratedOption.Identity
and it will be never modified by your application.