实体框架中的XML序列化

时间:2012-01-13 16:10:13

标签: xml entity-framework orm xml-serialization poco

Entity Framework是否能够使用模型对象,该模型对象包含一些在CRUD操作期间应该序列化/反序列化为xml的属性。

示例:

public class Question
    {
        public string Text { get; set; }
        public List<Answers> Answers { get; set; }
    }

public class Answers
    {
        public string Text { get; set; }
    }

作为插入的结果,我们应该在数据库中获得以下行:

Text           | Answers
_____________________________________________________________________________________
myQuestionText | <answers><answer Text="answer1"/><answer Text="answer2"/></answers>

1 个答案:

答案 0 :(得分:1)

没有。这不可能。你必须坚持这个课程:

public class Question
{
    public string Text { get; set; }
    public string Answers { get; set;}
}

自己处理序列化和反序列化。您可以使用自定义非映射属性(如果您使用EDMX文件使用您自己的分部类来定义属性)公开答案列表并隐藏属性的getter和setter中的序列化和反序列化逻辑。