2012-11-08 86 views
0

有兩個實體PostPhoto。兩者都有一個comment實體的集合。多個父實體首先在EF代碼中有一個子實體

反正是有避免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;} 
} 

回答

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

} 
相關問題