简体   繁体   中英

multiple parent Entities with one child entity in EF code first

there are tow entities Post and Photo . both have a collection of comment entity .

is there anyway to avoid the mapping written below (being have to define one property for each parent in Comment entity ) ?

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;}
}

You can do like this,

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;}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM