实体框架CodeFirst:在其他实体之间共享实体

时间:2016-01-14 21:22:53

标签: entity-framework

我在其他实体之间使用selector属性共享一个实体。 代码优先代码是:

public enum ContentTypes : byte
{
    EntityA = 0,
    EntityB = 1
}

public class SharedContent
{
    public int Id { get; set; }
    public ContentTypes ContentType { get; set; }

    public string Name { get; set; }
    public string Description { get; set; }
}

public class EntityA
{
    public int Id { get; set; }

    [ForeignKey("ContentId")]
    public virtual SharedContent Content { get; set; }
    public int ContentId { get; set; }
}

public class EntityB
{
    public int Id { get; set; }

    [ForeignKey("ContentId")]
    public virtual SharedContent Content { get; set; }
    public int ContentId { get; set; }
}

执行以下代码以从数据库实体框架获取entityA后,填充除entityA.Content之外的所有属性!怎么了?我需要什么来获得带有Content属性的entityA?

var entityA = context.EntityA.Include(e => e.Content).ToList();

1 个答案:

答案 0 :(得分:0)

我的错!在原始代码中,我在构造函数中创建内容。 有关详细说明,请点击链接...

EF codefirst : whether initialize navigation properties?

相关问题