EF的InverseProperty
直接进入正题吧
这是一个博客类型
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> BadPost { get; set; }
public virtual List<Post> GoodPosts { get; set; }
}
博客中有好的评论和坏的评论两个List()
那么我们评论类型就应该这样定义
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Url { get; set; }
[InverseProperty("BadPost")]
public Blog Blog { get; set; }
[InverseProperty("GoodPosts")]
public Blog Blog2 { get; set; }
}
也就使用到了InverseProoerty这个特性,给Blog值的时候就相当于这条评论给了BadPost这个集合,同样给Blog2值的时候就相当于这条评论给了GoodPost这个集合 ,接着我们看数据库的样子,Blog是什么样子就还是什么样子,Post生成的如下

数据库中存放的只是Blog的主键值。
浙公网安备 33010602011771号