![](/img/trans.png)
[英]How to map identifying relationship in Entity Framework 5 code first child entity with multiple mutually exclusive parent entities
[英]multiple parent Entities with one child entity in EF code first
有两个实体Post
和Photo
。 两者都有comment
实体的集合。
反正是为了避免下面写的mapping
(必须为Comment
实体中的每个父项定义一个属性)?
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;}
}
你可以这样做,
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;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.