简体   繁体   English

Ef Core 一对多与连接表

[英]Ef Core One-To-Many with join table

I have two models which both have a collection of the same third model.我有两个模型,它们都有相同的第三个 model 的集合。 Ef Core 5 now creates a foreign key for both models on the collection model but is there a way to let it generate a join table for each relationship without explicitly having a model for the join table? Ef Core 5 现在为集合 model 上的两个模型创建一个外键,但是有没有办法让它为每个关系生成一个连接表,而无需明确地为连接表创建一个 model?

My Models:我的模型:

public class Model1 {
    // ...
    public List<Model3> collection;
}

public class Model2 {
    // ...
    public List<Model3> collection;
}

public class Model3 {
    // ...
}

I want the db to look something like this:我希望数据库看起来像这样:

Table: Model1表:型号1

Table: Model2表:模型2

Table: Model3表:模型3

Table: Model3Model1 (JoinTable)表:Model3Model1(JoinTable)

  • Model3Id型号3Id
  • Model1Id型号1Id

Table: Model3Model2 (JoinTable)表:Model3Model2(JoinTable)

  • Model3Id型号3Id
  • Model2Id型号2Id

But I don't want explicit Types for those join tables.但我不想要那些连接表的显式类型。 I know that EFCore is able to infer those join tables for many-to-many relationship so I was wondering if there is a way to do this for one-to-many as well.我知道 EFCore 能够为多对多关系推断这些连接表,所以我想知道是否也有一种方法可以为一对多执行此操作。

I don't think it's possible.我不认为这是可能的。 The best compromise is private properties in Model3 like:最好的折衷方案是 Model3 中的私有属性,例如:

public class MyContext : DbContext
{
    public DbSet<Model1> Model1 { get; set; }
    public DbSet<Model2> Model2 { get; set; }
    public DbSet<Model3> Model3 { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Model3>()
            .HasMany<Model1>("Collection1")
            .WithMany(m1 => m1.Collection)
            .UsingEntity(j => {
                j.ToTable("Model3Model1");
                j.Property("Collection1Id").HasColumnName("Model3Id");
                j.Property("CollectionId").HasColumnName("Model1Id");
            });
        modelBuilder.Entity<Model3>()
            .HasMany<Model2>("Collection2")
            .WithMany(m2 => m2.Collection)
            .UsingEntity(j => {
                j.ToTable("Model3Model2");
                j.Property("Collection2Id").HasColumnName("Model3Id");
                j.Property("CollectionId").HasColumnName("Model2Id");
            });
    }
}

public class Model1
{
    public int Id { get; set; }
    public List<Model3> Collection { get; set; }
}

public class Model2
{
    public int Id { get; set; }
    public List<Model3> Collection { get; set; }
}

public class Model3
{
    public int Id { get; set; }
    private List<Model1> Collection1 { get; set; }
    private List<Model2> Collection2 { get; set; }
}

The generated join entity has a navigation property, where the name is + "Id".生成的连接实体有一个导航属性,其中名称为 +“Id”。 The tricky part is to set the desired column name.棘手的部分是设置所需的列名。

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

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