简体   繁体   English

Entity Framework Core 使用 ToTable 创建映射表

[英]Entity Framework Core use ToTable to create mapping table

I'm trying to create a mapping table using EF core and I had thought it was possible to create the mapping table without creating a specific entity.我正在尝试使用 EF 核心创建一个映射表,我认为可以在不创建特定实体的情况下创建映射表。

Currently I have EntityA and EntityB;目前我有 EntityA 和 EntityB; EntityA needs to be changed to have a collection of EntityB as below: EntityA 需要更改为具有 EntityB 的集合,如下所示:

EntityA
{
    int Id
    string Name
    ICollection<EntityB>
}

EntityB
{
    int Id
    string Name
}

Each entity is configured in its own class that implements IEntityTypeConfiguration每个实体都在自己的 class 中配置,实现IEntityTypeConfiguration

I want the resuling table to look like the following:我希望结果表如下所示:

Table: EntityA_EntityB_Mapping
int Id
int EntityAId
int EntityBId

Is it possible to use EntityTypeBuilder.ToTable on the fly without creating an separate entity for EntityAEntityB ?是否可以在不为EntityAEntityB创建单独实体的情况下即时使用 EntityTypeBuilder.ToTable ?

What's the proper way to achieve this?实现这一目标的正确方法是什么?

TIA TIA

I believe what you are referring to is you want:我相信你指的是你想要的:

public class EntityA
{
    public virtual ICollection<EntityB> Bs { get; set; }
}

public class EntityB
{
    public virtual ICollection<EntityA> As { get; set; }
}

rather than:而不是:

public class EntityA
{
    public virtual ICollection<EntityAB> ABs { get; set; }
}

public class EntityB
{
    public virtual ICollection<EntityAB> ABs { get; set; }
}

public class EntityAB
{
    public virtual EntityA A { get; set; }
    public virtual EntityB B { get; set; }
}

Which for a while was the only way to implement many-to-many relationships in EF Core.在一段时间内,这是在 EF Core 中实现多对多关系的唯一方法。 With EF Core 6 (and possibly 5?) you can implement the relationship directly either with a non-referenced joining entity (EntityAB but no dbSet and the collections still refer to EntityA or EntityB rather than the EntityAB) or with configuration just for the joining table.使用 EF Core 6(可能还有 5?),您可以直接使用未引用的加入实体(EntityAB 但没有 dbSet 和 collections 仍然引用 EntityA 或 EntityB 而不是 EntityAB)或仅用于加入的配置来实现关系桌子。

The detail for the options can be found in the EF Core documentation: https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key under the many-to-many relationship type config, but without creating a joining entity you can use:有关选项的详细信息,请参阅 EF Core 文档: https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key% 2Csimple-key在多对多关系类型配置下,但无需创建连接实体,您可以使用:

modelBuilder
    .Entity<EntityA>()
    .HasMany(x => x.Bs)
    .WithMany(x => x.As)
    .UsingEntity(a => a.ToTable("ABs"));

This needs to be expanded out if you want to configure a specific table structure for things like the PK/FK names.如果您想为 PK/FK 名称等配置特定的表结构,则需要将其扩展。 (Done within the UsingEntity ) If you define a joining entity you would set up the configuration for that EntityAB entity minus relationships then use .UsingEntity<EntityAB>() when configuring the A & B's. (在UsingEntity内完成)如果您定义一个加入实体,您将为该 EntityAB 实体设置配置减去关系,然后在配置 A 和 B 时使用.UsingEntity<EntityAB>()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM