简体   繁体   English

实体框架代码优先的多对多表外键

[英]Entity Framework code-first foreign key to many to many table

I have this Scenario: 我有这种情况:

public class Table1
{
    [Key]
    public string Table1Code { get; set; }

    public virtual List<Table2> Table2 { get; set; }
}

public class Table2
{
    [Key]
    public string Table2Code { get; set; }

    public virtual List<Table1> Table1 { get; set; }    
}

Then I create a configuration class for specifying the many to many table: 然后,我创建一个用于指定多对多表的配置类:

public class Table1Configuration : EntityTypeConfiguration<Table1>
{
    public Table1Configuration()
    {
        HasMany(g => g.Table2)
            .WithMany(r => r.Table1)
            .Map(c =>
            {
                c.ToTable("Table1_Table2");
                c.MapLeftKey("Table1Code");
                c.MapRightKey("Table2Code");
            });
    }
}

Now I have to create a Table3 like this 现在我必须创建一个这样的Table3

public class Table3
{
    [Key]
    public string Table3Code { get; set; }

    public string Table1Code { get; set; }
    public string Table2Code { get; set; }
}

How can I add the foreign key for columns Table1Code and Table2Code to the table Table1_Table2 ? 如何将表Table1CodeTable2Code列的外键添加到表Table1_Table2

I don't need to add the foreign key to Table1 and Table2 but to the table Table1_Table2 . 我不需要向Table1Table1 Table2添加外键,而是向表Table1_Table2添加外键。

Not sure you can do that without an explicit Table1_Table2 class: 不知道没有显式的Table1_Table2类,您可以做到这一点:

public class Table1_Table2
{
    public string Table1Code { get; set; }  // PK 1
    public string Table2Code { get; set; }  // PK 2
    public virtual Table3 Table3 { get; set; }    
}

Then: 然后:

public class Table3
{
    // Not needed in 1:1
    // [Key]
    // public string Table3Code { get; set; }
    public string Table1Code { get; set; }
    public string Table2Code { get; set; }
    // Make this a collection for 1:M
    public virtual Table1_Table2 Table1_Table2 { get; set; }    
}

Fluent code: 流利的代码:

modelBuilder.Entity<Table3>()
    .HasKey(t3 => new { t3.Table1Code, t3.Table2Code });

modelBuilder.Entity<Table1_Table2>()
    .HasOptional(t => t.Table3)
    .WithRequired(t3 => t3.Table1_Table2);

A Many2Many(M2M) Relation like you have made already using EF creates a table which has foregin keys to tables with entities in a M2M relation. 像您已经使用EF所做的Many2Many(M2M)关系创建了一个表,该表具有指向具有M2M关系中实体的表的前键。

So going by what you have done in classes Table1 and Table2 a third mapping table will be created by EF itself. 因此,按照您在类Table1Table2所做的操作,EF本身将创建第三个映射表。 So there is no special need to create a third table Table3 因此,没有特殊需要创建第三个表Table3

But if for domain reason you want to create a third mapping entity Table2 which maps Table1 and Table2 you will have to modify the classes in the following way. 但是,如果出于域原因,您要创建第三个映射Table1Table2映射实体Table2 ,则必须按以下方式修改类。

public class Table1
{
    [Key]
    public string Table1Code { get; set; }

    public virtual List<Table3> Table3 { get; set; }
}

public class Table2
{
    [Key]
    public string Table2Code { get; set; }

    public virtual List<Table3> Table3 { get; set; }    
}

 public class Table3
{
    [Key]
    public string Table3Code { get; set; }

    public Table1 Table1 { get; set; }
    public string Table1Code { get; set; }

    public Table2 Table2 { get; set; }
    public string Table2Code { get; set; }
}

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

相关问题 实体框架代码优先在同一个表上的多对多关系 - Entity Framework Code-first Many to many relationship on the same table 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 实体框架4代码优先多对多插入 - Entity Framework 4 Code-First many to many insert 代码优先实体框架-多对多关系 - Code-first Entity Framework - many-to-many relationships 实体框架5代码优先多对多表重新命名与继承类(TPC) - Entity Framework 5 Code-First Many-to-Many Table Re-naming with inherited classes (TPC) 连接表中的实体框架核心代码优先多对多属性 - Entity Framework Core code-first many-to-many properties in join table 实体框架代码优先设置的外键和同一表中的主键 - Entity Framework code-first set foreign key with primary key in same table 更新实体框架4.1代码优先中的外键关联 - Updating Foreign key associations in Entity Framework 4.1 Code-First 如何使用代码优先的实体框架指定外键 - How to specify a foreign key with code-first Entity Framework Oracle上的实体框架外键问题-代码优先 - Entity Framework foreign key problems on Oracle - code-first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM