[英]Entity Framework : Handle multiple relationships between two entities
There are two entities: 有两个实体:
"Other" person is designed in back-end who has no group. “其他”人是在没有团队的后端设计的。 (null) (空值)
A Yuvak - Person will always have one HomeGroup. Yuvak-个人将始终拥有一个家庭组。 (1=>1) And will have no groups to control. (1 => 1)并且将没有要控制的组。
A Nirikshak(Head) - Person will always have one HomeGroup. Nirikshak(负责人)-个人将始终拥有一个家庭组。 (1=>1) But he will also have multiple groups to control - GroupsOfNirikshak. (1 => 1)但他还将有多个组要控制-GroupsOfNirikshak。 (1=>Many) (1 =>多)
A Group will have multiple Yuvaks (1=>Many) 一个组将有多个Yuvaks(1 =>许多)
and all groups mostly will have only one Head. 并且所有组通常只有一个Head。 (Initially a new group might not have any head but zero or more yuvaks-persons.) (最初,一个新的组可能没有任何头,但是零个或更多的yuvaks人。)
[Table("Group")]
public class Group
{
[Key]
public int Id { get; set; }
.....
public virtual List<Yuvak> Yuvaks { get; set; }
[ForeignKey("Nirikshak")]
public int? NirikshakId { get; set; }
public virtual Yuvak Nirikshak { get; set; }
}
[Table("Yuvak")]
public class Yuvak
{
[Key]
public int Id { get; set; }
.....
[ForeignKey("HomeGroup")]
public int? HomeGroupId { get; set; }
public virtual Group HomeGroup { get; set; }
public virtual List<Group> GroupsOfNirikshak { get; set; }
}
I already has provided two foreign keys for 1=>1 relationships (nullable) in both entities. 我已经为两个实体中的1 => 1关系(可为空)提供了两个外键。 And now to manage many to many relationship it should automatically create a third table with "Yuvak_Id" and "Group_Id" columns if they are not null. 现在要管理多对多关系,如果它们不为null,则应自动创建带有“ Yuvak_Id”和“ Group_Id”列的第三个表。 But as here the FKs are null; 但是这里的FK为空; instead of creating a third table it adds a foreign key column in both the tables.(Group:"Yuvak_Id" and Yuvak:"Group_Id") 而不是创建第三个表,而是在两个表中都添加了一个外键列。(组:“ Yuvak_Id”和Yuvak:“ Group_Id”)
What should I do so that to maintain Yuvak-HomeGroup it should use above provided foreign keys only and for many to many relationship (Head-GroupsOfNirikshak & Group-Yuvaks )it should create a seperate table. 我应该怎么做才能维护Yuvak-HomeGroup,它应该仅使用上面提供的外键,并且对于多对多关系(Head-GroupsOfNirikshak和Group-Yuvaks),它应该创建一个单独的表。
Can I create a separate table for many to many relationship like : (ID,YuvakID,GroupID) How can I do that? 我可以为多对多关系创建一个单独的表,例如:(ID,YuvakID,GroupID)我该怎么做?
I tried to do that but got different errors like below : 我试图这样做,但是遇到了如下不同的错误:
..... .....
and so on. 等等。
Use the "Fluent Api" ! 使用“ Fluent Api”!
In your context class write (for example) : 在您的上下文类中编写(例如):
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Group>()
.HasOptional(t => t.Nirikshak)
.WithMany(t => t.GroupsOfNirikshak)
.HasForeignKey(t => t.NirikshakId);
}
You can remove the annotations on the classes and properties. 您可以删除类和属性上的注释。 Once you have a Model-based Class on another, Entity Framework will automatically create a foreign key relationship on it. 一旦在另一个上具有基于模型的类,则Entity Framework将自动在其上创建外键关系。 It will process Yuvak as a node on the Group Graph object. 它将把Yuvak处理为Group Graph对象上的一个节点。 You need not declare the annotations since EF will do that for you automatically. 您无需声明注释,因为EF会自动为您这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.