实体框架中的 XML 序列化

发布于 2024-12-27 01:09:31 字数 575 浏览 3 评论 0原文

实体框架是否能够使用包含一些在 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>

Is Entity Framework able to work with a model object which contains some properties that should be serialized/deserialized into xml during CRUD operations.

Example:

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

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

As a result of insert we should get the following row in the database:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2025-01-03 01:09:31

不,这是不可能的。你必须持久化这个类:

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

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

No. It is not possible. You must persist this class:

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

And handle serialization and deserialization by yourselves. You can use custom non mapped property (if you are using EDMX file use your own partial class to define the property) exposing list of answers and hide serialization and deserialization logic inside property's getter and setter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文