将自定义集合的属性映射到EF中的列

时间:2018-05-11 08:52:16

标签: entity-framework entity-framework-6

我喜欢这堂课:

public class InvertedList<T> : List<T>{
    public bool IsInverted { get; set; }
}

在像这样的实体中使用时:

public class Parent {
    public InvertedList<Child> Children { get; set; }
}

映射到数据库表,如

CREATE TABLE Parent (
    Child_IsInverted bit
);

CREATE TABLE Child (
    ParentId int
)

我尝试将[Column]放在InvertedList类的IsInverted属性[ComplexType]上,但始终忽略该属性。

有没有办法做这样的事情或类似的事情? 当然我可以像

那样手动完成
public class Parent {
    public bool Child_IsInverted { get; set; }
    public List<Child> Children { get; set; }
}

但是我真的不喜欢在Parent实体上放置所有那些Child_IsInverted属性(我将有很多这样的可反转列表)。我能想到的至少部分实现这一点的唯一方法是使用单独的域和数据库模型,并使用存储库对其进行转换 - 这样我可以使用理想的模型,但看起来有点太多的努力这样的任务。你能提供其他选择吗?

1 个答案:

答案 0 :(得分:0)

当EF反序列化List&lt;&gt;时(一切实现ICollection&lt;&gt;)它不会序列化除内容之外的其他属性。

只是一个建议,Compex类型可能是一个选项不支持泛型,不支持导航属性,你在这里有两个。

相关问题