简体   繁体   English

具有实体框架代码的XML字段优先

[英]XML Fields with Entity Framework Code First

I'm using Entity Framework with the Code First model (pet project, and I love editing simple classes and having my schema updated automatically). 我正在使用Entity Framework和Code First模型(宠物项目,我喜欢编辑简单的类并自动更新我的模式)。 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. 另一方面,如果我将一个带有5,000个点的多边形保存到数据库中,则需要永远运行那么多插入,老实说,除了检索单个多边形之外,我永远不会查询点。

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 我喜欢做的是摆脱“Point”类中的“PolygonId”,摆脱“Points”表,让Polygon表看起来像

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? 有没有办法让EF执行此操作,或者为字段编写自定义序列化器/反序列化器,所以至少在整个代码库中使用时它似乎是自动的?

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. EditorBrowsableDebuggerBrowsable属性是可选的,只是保持XML属性不会出现在Intellisense中(当从库中使用此类型时)和调试器。

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

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