与实体框架代码优先领域XML [英] XML Fields with Entity Framework Code First

查看:96
本文介绍了与实体框架代码优先领域XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架与代码优先模式(宠物项目,我喜欢简单的编辑类和有我的模式自动更新)。我有一个类类似如下:



<预类=郎-CS prettyprint-覆盖> [表(多边形)]
公共类多边形
{
公众诠释PolygonId {搞定;组; }
公共字符串纹理{搞定;组; }

公共虚拟的ICollection<点和GT;点{搞定;组; }
}

表(点)]
公共类点
{
公众诠释PolygonId {搞定;组; }
公共双X {搞定;组; }
公共双Y {搞定;组; }
}



这对我多边形存储在数据库中,并且能够很有用查询其质感。在另一方面,如果我用节省5000点多边形到数据库需要永远运行,许多插入,说实话,我永远不会被查询点除了检索单个多边形。



我很乐意做点类摆脱PolygonId的,摆脱了点表,并有多边形查表类似

  PolygonId INT PK 
纹理VARCHAR(255)
点XML

和然后让点只是序列到直接保存到表中的字符串,但是unserializes回点的阵列。有没有办法要么已经EF做到这一点,或者编写领域的定制串行器/解串器,所以至少在整个代码库中使用时,似乎自动?



谢谢,




解决。方案

我认为你必须添加另一个属性和写一些代码来执行序列化



这应该这样做:

  [表(多边形)] 
公共类多边形
{
公众诠释PolygonId {得到;组; }
公共字符串纹理{搞定;组; }

[NotMapped]
公共虚拟的ICollection<点和GT;点{搞定;组; }

[列(点)]
[EditorBrowsable(EditorBrowsableState.Never)
[DebuggerBrowsable(DebuggerBrowsableState.Never)
公共字符串PointsXml
{
得到
{
无功序列化=新的XmlSerializer(typeof运算(列表<点和GT;));
使用(VAR的StringWriter =新的StringWriter())
{
serializer.Serialize(StringWriter的,Points.ToList());
stringWriter.Flush();
返回stringWriter.ToString();
}
}

{
无功序列化=新的XmlSerializer(typeof运算(列表<点和GT;));
使用(VAR stringReader =新StringReader(值))
{
点=(列表<点和GT;)serializer.Deserialize(stringReader);
}
}
}
}



EditorBrowsable DebuggerBrowsable 属性是可选的,只要保持XML属性的动态在智能感知(当使用这种类型从库)和调试器。


I'm using Entity Framework with the Code First model (pet project, and I love editing simple classes and having my schema updated automatically). I have a class like follows:

[Table("Polygons")]
public class Polygon
{
    public int PolygonId { get; set; }
    public String Texture { get; set; }

    public virtual ICollection<Point> Points { get; set; }
}

[Table("Points")]
public class Point
{
    public int PolygonId { get; set; }
    public double X { get; set; }
    public double Y { get; set; }
}

It's useful to me to store Polygons in the database, and be able to query their texture. On the other hand, if I'm saving a polygon with 5,000 points to the database it takes forever to run that many inserts, and honestly, I'm never going to be querying Points except to retrieve an individual Polygon.

What I'd love to do is get rid of "PolygonId" in the "Point" class, get rid of the "Points" table, and have the Polygon table look something like

PolygonId int PK
Texture varchar(255)
Points XML

And then have the points just serialize to a string that is saved directly into the table, yet unserializes back into an array of points. Is there a way to either have EF do this, or to write a custom serializer/deserializer for the field, so at least it seems automatic when used throughout the code-base?

Thanks,

Dan

解决方案

I think that you would have to add another property and write some code to do the serialization.

This should do it:

[Table("Polygons")]
public class Polygon
{
    public int PolygonId { get; set; }
    public String Texture { get; set; }

    [NotMapped]
    public virtual ICollection<Point> Points { get; set; }

    [Column("Points")]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public string PointsXml
    {
        get
        {
            var serializer = new XmlSerializer(typeof (List<Point>));
            using (var stringWriter = new StringWriter())
            {
                serializer.Serialize(stringWriter, Points.ToList());
                stringWriter.Flush();
                return stringWriter.ToString();
            }
        }
        set
        {
            var serializer = new XmlSerializer(typeof(List<Point>));
            using (var stringReader = new StringReader(value))
            {
                Points = (List<Point>) serializer.Deserialize(stringReader);
            }
        }
    }
}

The EditorBrowsable and DebuggerBrowsable attributes are optional and will just keep the XML property from showing up in Intellisense (when this type is used from a library) and the debugger.

这篇关于与实体框架代码优先领域XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆