[英]Entity Framework 5 and XElement fields
Started playing with Visual Studio 2012 RC and Entity Framework 5... absolutely loving it but wondering if there's a cleaner way to do this. 开始使用Visual Studio 2012 RC和Entity Framework 5 ...绝对喜欢它,但想知道是否有更清洁的方法来做到这一点。
I would like to cut out the middle-man of parsing the XML every time, and setting it via .ToString() 我想减少每次分析XML并通过.ToString()进行设置的中间人
public class MyEFEntity
{
[NotMapped()]
public XElement Tags {
get { return XElement.Parse(tags); }
set { tags = value.ToString(); } }
[Column("Tags", TypeName = "xml"), Required]
public string tags { get; set; }
}
In principle there is no better way. 原则上没有更好的方法。 You need two properties - one for XElement
and one for backing persisted string. 您需要两个属性-一个用于XElement
,另一个用于支持持久字符串。 If you want to reduce amount of parsing and conversion you need to implement some infrastructure for that. 如果要减少解析和转换的数量,则需要为此实现一些基础结构。 General approach would be: 一般方法是:
ObjectContext.ObjectMaterialized
event - if the materialized object is MyEFEntity
parse string and save it to XElement
property. 处理ObjectContext.ObjectMaterialized
事件-如果实例化对象是MyEFEntity
解析字符串,然后将其保存到XElement
属性。 If you are using DbContext
you can still get ObjectContext
through its explicitly implemented IObjectContextAdapter
. 如果使用的是DbContext
,仍然可以通过其显式实现的IObjectContextAdapter
获得ObjectContext
。 SaveChanges
- in the method find all modified or inserted instances of MyEFEntity
through DbContext.ChangeTracker.GetEntries
and save their XML to string property 覆盖SaveChanges
在方法中,通过DbContext.ChangeTracker.GetEntries
查找所有已修改或插入的MyEFEntity
实例,并将其XML保存到字符串属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.