Entity Framework 多对多关系 3 类
Entity Framework Many to Many Relationship 3 Classes
您好,我有以下三个 classes。我正在尝试使用三个 classes 实现多对多映射。我已经实现了两个 class 之间的多对多关系,但我正试图在混合中获得另一个 class。下面是我拥有的 classes 和我试图实现的关系的 class 表示。
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmployeeId { get; set; }
public ICollection<Department> Departments { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public string DepartmentCode { get; set; }
public string Description { get; set; }
public ICollection<Person> Members { get; set; }
}
public class Role
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
关于如何实现以下目标的任何提示都会有所帮助。
public class PersonRoleDepartment
{
public int PersonID { get; set; }
public int DepartmentID{ get; set; }
public int RoleID { get; set; }
}
public class Person
{
public int Id { get; set; }
public ICollection<PersonDepartmentRole> DepartmentRoles { get; set; }
}
public class Department
{
public int Id { get; set; }
public ICollection<PersonDepartmentRole> PersonRoles { get; set; }
}
public class Role
{
public int Id { get; set; }
public ICollection<PersonDepartmentRole> PersonDepartments { get; set; }
}
public class PersonDepartmentRole
{
[Key, Column( Order = 0 )]
public int PersonId { get; set; }
[ForeignKey( "PersonId" )]
[Required]
public virtual Person Person { get; set; }
[Key, Column( Order = 1 )]
public int DepartmentId { get; set; }
[ForeignKey( "DepartmentId" )]
[Required]
public virtual Department Department { get; set; }
[Key, Column( Order = 2 )]
public int RoleId { get; set; }
[ForeignKey( "RoleId" )]
[Required]
public virtual Role Role { get; set; }
}
流畅API 配置:
var pdrConfig = modelBuilder.Entity<PersonDepartmentRole>()
.HasKey( pdr => new
{
pdr.PersonId,
pdr.DepartmentId,
pdr.RoleId
} );
pdrConfig.HasRequired( pdr => pdr.Department )
.WithMany( d => d.PersonRoles )
.HasForeignKey( pdr => pdr.DepartmentId );
pdrConfig.HasRequired( pdr => pdr.Person )
.WithMany( p => p.DepartmentRoles )
.HasForeignKey( pdr => pdr.PersonId );
pdrConfig.HasRequired( pdr => pdr.Role )
.WithMany( r => r.PersonDepartments )
.HasForeignKey( pdr => pdr.RoleId );
注意:从任何主要实体到其他实体的所有引用都应通过您的联结实体 PersonDepartmentRole
- 否则,可能会出现数据不一致(例如,我将 Department
添加到 Person
以及相应的 PersonDepartmentRole
记录到数据库,但随后我从 Person
实体中删除了 Department
- PersonDepartmentRole
实体仍然存在)
相反,如果您想要一个人的所有部门:
db.Person.DepartmentRoles.Select( dr => dr.Department ).Distinct()
在这个构造中,一个人可以在一个部门但没有角色吗?或者,一个部门没有人,能发挥作用吗?如果没有,这怎么可能完成
您好,我有以下三个 classes。我正在尝试使用三个 classes 实现多对多映射。我已经实现了两个 class 之间的多对多关系,但我正试图在混合中获得另一个 class。下面是我拥有的 classes 和我试图实现的关系的 class 表示。
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmployeeId { get; set; }
public ICollection<Department> Departments { get; set; }
}
public class Department
{
public int ID { get; set; }
public string Name { get; set; }
public string DepartmentCode { get; set; }
public string Description { get; set; }
public ICollection<Person> Members { get; set; }
}
public class Role
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
关于如何实现以下目标的任何提示都会有所帮助。
public class PersonRoleDepartment
{
public int PersonID { get; set; }
public int DepartmentID{ get; set; }
public int RoleID { get; set; }
}
public class Person
{
public int Id { get; set; }
public ICollection<PersonDepartmentRole> DepartmentRoles { get; set; }
}
public class Department
{
public int Id { get; set; }
public ICollection<PersonDepartmentRole> PersonRoles { get; set; }
}
public class Role
{
public int Id { get; set; }
public ICollection<PersonDepartmentRole> PersonDepartments { get; set; }
}
public class PersonDepartmentRole
{
[Key, Column( Order = 0 )]
public int PersonId { get; set; }
[ForeignKey( "PersonId" )]
[Required]
public virtual Person Person { get; set; }
[Key, Column( Order = 1 )]
public int DepartmentId { get; set; }
[ForeignKey( "DepartmentId" )]
[Required]
public virtual Department Department { get; set; }
[Key, Column( Order = 2 )]
public int RoleId { get; set; }
[ForeignKey( "RoleId" )]
[Required]
public virtual Role Role { get; set; }
}
流畅API 配置:
var pdrConfig = modelBuilder.Entity<PersonDepartmentRole>()
.HasKey( pdr => new
{
pdr.PersonId,
pdr.DepartmentId,
pdr.RoleId
} );
pdrConfig.HasRequired( pdr => pdr.Department )
.WithMany( d => d.PersonRoles )
.HasForeignKey( pdr => pdr.DepartmentId );
pdrConfig.HasRequired( pdr => pdr.Person )
.WithMany( p => p.DepartmentRoles )
.HasForeignKey( pdr => pdr.PersonId );
pdrConfig.HasRequired( pdr => pdr.Role )
.WithMany( r => r.PersonDepartments )
.HasForeignKey( pdr => pdr.RoleId );
注意:从任何主要实体到其他实体的所有引用都应通过您的联结实体 PersonDepartmentRole
- 否则,可能会出现数据不一致(例如,我将 Department
添加到 Person
以及相应的 PersonDepartmentRole
记录到数据库,但随后我从 Person
实体中删除了 Department
- PersonDepartmentRole
实体仍然存在)
相反,如果您想要一个人的所有部门:
db.Person.DepartmentRoles.Select( dr => dr.Department ).Distinct()
在这个构造中,一个人可以在一个部门但没有角色吗?或者,一个部门没有人,能发挥作用吗?如果没有,这怎么可能完成