簡體   English   中英

多個父實體首先在EF代碼中包含一個子實體

[英]multiple parent Entities with one child entity in EF code first

有兩個實體PostPhoto 兩者都有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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM