在M到N关系中使用InverseProperty

时间:2013-03-21 22:29:51

标签: entity-framework

让我们说一个人可以属于几个群体,一个群体可以包含几个人。
一个人也可以管理几个团体,一个团体可以由几个人管理。

如何向这些属性添加一些属性(InverseProperty),以便它们彼此正确匹配?

public class Person
{
   public virtual ICollection<Group> Groups { get; set; }
   public virtual ICollection<Group> AdministratedGroups { get; set; }
}

public class Group
{
   public virtual ICollection<Person> Members { get; set; }
   public virtual ICollection<Person> Administrators { get; set; }
}

由于

1 个答案:

答案 0 :(得分:0)

最后很明显:

public class Person
{
   [InverseProperty("Members")]
   public virtual ICollection<Group> Groups { get; set; }
   [InverseProperty("Administrators")]
   public virtual ICollection<Group> AdministratedGroups { get; set; }
}

public class Group
{
   [InverseProperty("Groups")]
   public virtual ICollection<Person> Members { get; set; }
   [InverseProperty("AdministratedGroups")]
   public virtual ICollection<Person> Administrators { get; set; }
}