有两个实体Post
和Photo
。两者都有comment
实体的集合。
无论如何都要避免下面写的mapping
(必须为Comment
实体中的每个父级定义一个属性? / p>
public class Post
{
public string Title {get; set;}
public ICollection<Comment> Comments{get; set; }
}
public class Photo
{
public string Path{get;set;}
public ICollection<Comment> Comments{get; set; }
}
public class Comment
{
public int? PostId{get;set;}
public Virtual Post Post{get;set;}
public int? PhotoId{get;set;}
public Virtual Photo Photo{get;set;}
}
答案 0 :(得分:2)
你可以这样做,
public class PostBase{
public ICollection<Comment> Comments{get; set; }
}
public class Post:PostBase
{
public string Title {get; set;}
}
public class Photo:PostBase
{
public string Path{get;set;}
}
public class Comment
{
public int? PostBaseId{get;set;}
public Virtual PostBase PostBase{get;set;}
}