2012-01-13 75 views
1

實體框架能夠與模型對象一起工作,該模型對象包含一些在CRUD操作期間應該序列化/反序列化爲xml的屬性。實體框架中的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

號這是不可能的。你必須堅持這個類:

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

並自己處理序列化和反序列化。您可以使用自定義非映射屬性(如果您使用EDMX文件,請使用您自己的部分類來定義屬性),公開隱藏答案列表並隱藏屬性的getter和setter中的序列化和反序列化邏輯。