简体   繁体   English

在使用EF Fluent API映射我的实体关系时,我是否需要同时映射涉及的两个实体?

[英]When using EF Fluent API to map my entities relationships, do I need to map on both entities involved?

I'm creating the entity relationships using Code First with EF Fluent API following the instructions from this tutorial to create the 1:n and m:n relationships in my model. 我将按照本教程中的说明,使用带有EF Fluent API的Code First创建实体关系,以在模型中创建1:n和m:n关系。

What I'm wondering and haven't found a response searching, is that if it's required to define the relationship on both ends of it. 我想知道但尚未找到响应搜索的是,是否需要在它的两端定义关系。

By that I mean, if I have an entity called User and Organization, there are two relationships involving these two entities as I described below: 我的意思是,如果我有一个名为“用户和组织”的实体,则有两个关系涉及这两个实体,如下所述:

  • An User can many Organizations, while an Organization must be owned by one User. 一个用户可以有多个组织,而一个组织必须由一个用户拥有。
  • An User may be present in many Organizations, while an Organization may have many Users in it. 一个用户可能存在于许多组织中,而一个组织中可能有许多用户。

With this in mind, I defined the relationships using Fluent API as follows: 考虑到这一点,我使用Fluent API定义了如下关系:

        modelBuilder.Entity<Organization>().HasRequired(o => o.Owner).WithMany(u => u.OrganizationsOwned).WillCascadeOnDelete(false);

        modelBuilder.Entity<User>().HasMany<Organization>(u => u.Organizations).WithMany(o => o.Users).Map(uo =>
        {
            uo.MapLeftKey("UserId");
            uo.MapRightKey("OrganizationId");
            uo.ToTable("OrganizationsUsers");
        });

But are these definitions enough? 但是这些定义够吗? Or do I have to define the relationships on the other end of the entities too? 还是我也必须在实体的另一端定义关系? What I mean, do I need to add the following code? 我的意思是,我需要添加以下代码吗?

        modelBuilder.Entity<User>().HasMany(u => u.OrganizationsOwned).WithRequired(o => o.Owner).WillCascadeOnDelete(false);

        modelBuilder.Entity<Organization>().HasMany<User>(o => o.Users).WithMany(u => u.Organizations).Map(ou =>
        {
            ou.MapLeftKey("UserId");
            ou.MapRightKey("OrganizationId");
            ou.ToTable("OrganizationsUsers");
        });

Yes, these definitions are enough. 是的,这些定义就足够了。 You are defining both sides of the relationship with the single statement: 您将使用单个语句定义关系的两侧:

.Entity<Organization>().HasRequired(o => o.Owner).WithMany(u => u.OrganizationsOwned)

The .HasRequired defines the 1 side, and the .WithMany defines the Many side. .HasRequired定义1面,.WithMany定义Many面。 There is only one relationship, but two sides. 只有一种关系,但是只有两种关系。 You can define the relationship from either side, but you do not have to define it from both. 您可以从任何一侧定义关系,但是不必从双方都定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF6 Fluent API映射自定义键/属性上的相关实体非PK - EF6 Fluent API Map Related Entities On Custom Key/Properties Not PK 如何在EF 6中的Fluent API中映射1到0或1的关系 - How do I map a 1 to 0 or 1 relationship in Fluent API in EF 6 C#ef映射实体 - C# ef map entities 如何使用Fluent NHibernate映射包含与父类型相同类型的实体的List? - How can I map a List which contains entities of same type as of parent type using Fluent NHibernate? EF6 Fluent Api-我需要在FluentApi中设置默认值才能映射要使用的子级而不是父级 - EF6 Fluent Api - I need to set Default value in FluentApi in order to Map Child to be used instead of Parent DDD以及如何将EF实体映射到域对象? - DDD and how to map EF entities to domain objects? 是否可以将 map 存储过程存储到 EF Core 中的实体? - Is it possible to map stored procedures to entities in EF Core? 使用Fluent API配置1对1以及3个实体中的多对多实体 - Configure 1 to 1 and many to many in 3 Entities using Fluent API 我是否需要使用EntityFramework更新一对多关系中的两个实体? - Do I need to update both Entities in a one-to-many relationship using EntityFramework? EF:使用流畅的 API 映射基类属性 - EF: Map base class property with fluent API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM