简体   繁体   English

更新实体框架中的Computed列

[英]Update Computed column in entity framework

I have a DateTime column in my app that gets auto calculated by SQLServer. 我的应用程序中有一个DateTime列,由SQLServer自动计算。 It works great when creating the row. 它在创建行时效果很好。 However, I would like to be able to updated later. 但是,我希望以后能够更新。 I have the property marked with [DatabaseGenerated(DatabaseGeneratedOption.Computed)] . 我有标有[DatabaseGenerated(DatabaseGeneratedOption.Computed)]的属性。

Is it possible to updated a property like that in EF? 是否可以更新EF中的属性?

No, with DatabaseGeneratedOption.Computed EF will never include the property in update and insert statements. 不,使用DatabaseGeneratedOption.Computed EF将永远不会在update和insert语句中包含该属性。 In stead, it will always read its value after such statements. 相反,在这些陈述之后,它将始终读取其价值。

If you have to update the property in client code you have no other option than removing the data annotation an make it an ordinary updateable property. 如果必须在客户端代码中更新属性,除了删除数据注释之外没有其他选择,请将其设置为普通的可更新属性。 You can set a default value in the owning class's constructor. 您可以在拥有类的构造函数中设置默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM