简体   繁体   English

实体框架:处理两个实体之间的多个关系

[英]Entity Framework : Handle multiple relationships between two entities

There are two entities: 有两个实体:

  1. Group
  2. Yuvak - Person Yuvak-人

"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 : 我试图这样做,但是遇到了如下不同的错误:

  1. The navigation property 'HomeGroup' declared on type 'YTKGoshthiManagment.Models.Yuvak' has been configured with conflicting foreign keys. 在类型“ YTKGoshthiManagment.Models.Yuvak”上声明的导航属性“ HomeGroup”已配置有冲突的外键。
  2. Multiplicity is not valid in Role 'Yuvak_HomeGroup_Target' in relationship 'Yuvak_HomeGroup'. 多重性在关系“ Yuvak_HomeGroup”中的角色“ Yuvak_HomeGroup_Target”中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. 因为从属角色属性不是关键属性,所以从属角色多重性的上限必须为'*'。

..... .....

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.

相关问题 如何在 Entity Framework Core 中处理两个表之间的多个关系? - How to handle multiple relationships between two tables in Entity Framework Core? 实体框架核心 - 两个实体之间的多个一对多关系 - Entity Framework Core - Multiple one-to-many relationships between two entities 实体框架:ObjectSet attach(),具有多个关系的实体 - Entity Framework: ObjectSet attach(), entities with multiple relationships 实体框架 6:实体和关系 - Entity Framework 6: entities and relationships 在 Entity Framework Core 中,如果我定义这些关系,它会在两个实体之间创建循环关系吗? - In Entity Framework Core, if I define these relationships, will it create a cyclic relationship between two entities? 更新具有与其他实体的多个关系的实体框架实体 - Update entity framework entity with multiple relationships to other entities 如何定义两个实体之间的多个关系? - How to define multiple relationships between two entities? 实体框架两个视图之间的关系 - Entity Framework Relationships between two views 实体框架代码优先关系:一对多到多个实体 - Entity Framework Code First Relationships: One to Many to Multiple Entities 管理2个实体之间的两个关系 - Managing two relationships between 2 entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM