通过Entity Framework映射嵌套类

时间:2013-03-04 21:26:48

标签: entity-framework

我有这个定义的课程:

public class WebSiteContent 
    {
        public Guid Id { get; set; }
        public About About { get; set; }
        public Tips Tips { get; set; }
        public Images Images { get; set; }
    }

我的About和Tips and Images看起来像这样:

 public class About
    {
        public Guid Id { get; set; }
        public string Text { get; set; }
        public string Addres { get; set; }
        public int PhoneNumber { get; set; }
        public int Mobile { get; set; }
    }

和提示:

 public class Tips
    {
        public Guid Guid { get; set; }
        public string Content { get; set; }
    }

和图片:

public class Images
    {
        public Guid Id { get; set; }
        public string Background { get; set; }
        public string Logo { get; set; }
        public About About { get; set; }

    }

这里我只是想使用about和Images和提示作为帮助类来创建一个属性,并且不希望在数据库中有关于,图像或提示表!

实体框架需要Id来映射以上所有类,我该怎么做?

1 个答案:

答案 0 :(得分:1)

  

这里我只想使用about和Images以及提示作为帮助类   只是创建一个属性,不想有关于,图像或提示   数据库中的表

所以你正在寻找复杂的类型。使用[ComplexType]属性标记您的About,Tips和Images类。

  

实体框架需要Id来映射以上所有类,我该怎么办   那个?

EF仅需要实体的Id。如果将它们映射为复杂类型,则无需使用任何Id。

顺便说一下。如果您不想在数据库中拥有这些类及其属性,则可以使用[NotMapped]属性。

相关问题