0

有两个实体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;}
}
4

1 回答 1

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

}
于 2012-11-08T06:25:36.397 回答