内容简介:我正在使用Entity Framework 4.1 Code First.是否有内置的方式来获取从数据库加载实体以来哪些属性已更改的列表?我知道代码首先检测到一个对象被改变了,但是有没有办法得到什么属性已经改变了?代码日志版权声明:翻译自:http://stackoverflow.com/questions/7114333/ef-4-1-code-first-determine-what-properties-have-changed
我正在使用Entity Framework 4.1 Code First.是否有内置的方式来获取从数据库加载实体以来哪些属性已更改的列表?我知道代码首先检测到一个对象被改变了,但是有没有办法得到什么属性已经改变了?
对于标量和复杂属性,您可以使用以下内容来提取更改的实体属性名称myEntity:
var entry = context.Entry(myEntity);
var namesOfChangedProperties = entry.CurrentValues.PropertyNames
.Where(p => entry.Property(p).IsModified);
这里需要注意的几点:
> CurrentValues.PropertyNames仅包含标量和复杂属性,而不包含导航属性.
>复杂属性意味着:只有在实体上声明的复杂属性的名称,而不是复杂类型本身的实际单个属性,例如:如果你有这个模型…
[ComplexType]
public class Address
{
public string Country { get; set; }
public string City { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
…然后,如果myEntity是一个人,CurrentValues.PropertyNames将包含“Id”,“Name”和“Address”,但不包含“Address.Country”或“Address.City”(或“Country”或“City”) .
>如果一个复杂的属性被标记为已修改(在上面的代码中的IsModified是true),那么这意味着引用(上面示例中的Person.Address)已经改变,无论实际的属性值(Country和City)内部的复杂类型有没有改变.或者复合类型的任何属性已更改(国家或城市已更改).我相信不可能找出哪一个,因为EF总是向数据库发送所有复杂类型属性的UPDATE命令,即使只有一个属性已更改,另一个属性保持不变.我从此得出结论,EF不追踪单个复杂类型属性的变化.
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/7114333/ef-4-1-code-first-determine-what-properties-have-changed
以上所述就是小编给大家介绍的《c# – EF 4.1代码优先 – 确定哪些属性已更改》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- c# – 使用nameof操作符而不是CallerMemberNameAttribute来通知.NET 4.5.3中的属性更改有什么好处吗?
- MongoDB更改oplog大小
- ios – 更改NSURL的方案
- Vagrant更改默认的SSH端口
- Firefox 75.0 发布,地址栏更改
- Vagrant更改默认的SSH端口
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Numerical Methods and Methods of Approximation in Science and En
Karan Surana / CRC Press / 2018-10-31
ABOUT THIS BOOK Numerical Methods and Methods of Approximation in Science and Engineering prepares students and other readers for advanced studies involving applied numerical and computational anal......一起来看看 《Numerical Methods and Methods of Approximation in Science and En》 这本书的介绍吧!