[英]Custom TimeStamp property in EF code first
我在项目中首先使用EF 6.1代码。 对于以下实体,当仅更改Code
和“ Name
值时,我想增加数据库中“ Version
字段的值(在断开连接的情况下更新我的实体):
public class Product
{
public int Id {get; set;}
public string Code {get; set;}
public string Name {get; set;}
public string Title {get; set;}
public int Version {get; set;}
}
当实体的任何属性值更改时,使用[TimeStamp]
注释会导致[TimeStamp]
属性更改。
有什么办法可以在EF中完成这项工作?
您可以检查属性状态是否已更改。 这里的MSDN链接
您更新的代码可能如下所示。
public void Update(Product product) {
if (context.Entry(product).Property(u => u.Code).IsModified && context.Entry(product).Property(u => u.Name).IsModified) {
product.Version += 1;
}
context.Entry(product).State = System.Data.Entity.EntityState.Modified;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.