【发布时间】:2019-02-14 14:21:23
【问题描述】:
这个问题可能很简单,但逻辑很重要,我对此感到困惑。在带有实体框架核心代码的 Asp.Net Core 2.1 中,我想学习如何建模,所以我简化了这个问题。两个不同实体(中心和文章)中的一个相同导航属性(照片)。中心可以有很多照片,文章可以有一张照片。一张照片可以有一个帖子或一个中心,因此也可以有一个 MyEntityBase。示例:
public class Photo
{
public int Id { get; set; }
public string Url { get; set; }
//The question/relation problem is here???
//public int CenterId { get; set; }
//public virtual Center Center { get; set; }
//public int ArticleId { get; set; }
//public virtual Article Article{ get; set; }
//public int MyEntityBaseId { get; set; }
//public virtual MyEntityBase ArticleOrPost{ get; set; }
}
public class Article: MyEntityBase
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
//Common Photo property
//One article has one photo
public virtual Photo ArticlePhoto { get; set; }
}
public class Center: MyEntityBase
{
[Key]
public int Id { get; set; }
public string Name{ get; set; }
//Common Photo property
//One center has many photo
public virtual List<Photo> CenterPhotos { get; set; }
}
【问题讨论】:
标签: c# asp.net-core ef-code-first entity-framework-core