[英]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
? 如何将表
Table1Code
和Table2Code
列的外键添加到表Table1_Table2
?
I don't need to add the foreign key to Table1
and Table2
but to the table Table1_Table2
. 我不需要向
Table1
和Table1
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. 因此,按照您在类
Table1
和Table2
所做的操作,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. 但是,如果出于域原因,您要创建第三个映射
Table1
和Table2
映射实体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.