2016-01-14 411 views
1

我正在使用選擇器屬性在其他實體之間共享一個實體。 的Code First代碼:實體框架CodeFirst:在其他實體之間共享實體

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(); 
+0

嘗試使用「[關鍵]」屬性標識 – Miller

+0

謝謝,但我已經使用關鍵屬性。我會更新示例代碼。 – gDir

回答