C# 在只有一对零或一关系的表上设置复合键
使用EF6,我试图定义以下关系A1--0..1C0..1--1B x是基数。然后,表C将使用来自a和B的2个外键的组合键C# 在只有一对零或一关系的表上设置复合键,c#,entity-framework,ef-code-first,fluent,composite-key,C#,Entity Framework,Ef Code First,Fluent,Composite Key,使用EF6,我试图定义以下关系A1--0..1C0..1--1B x是基数。然后,表C将使用来自a和B的2个外键的组合键 // Other properties are omitted for brevity public class A { public int Id { get; set; } public virtual C C { get; set; } } public class B { public int Id { get; set; } pub
// Other properties are omitted for brevity
public class A
{
public int Id { get; set; }
public virtual C C { get; set; }
}
public class B
{
public int Id { get; set; }
public virtual C C { get; set; }
}
public class C
{
public virtual A A { get; set; }
public virtual B B { get; set; }
}
这是我设计的fluent API
public class C : EntityTypeConfiguration<C>
{
public C()
{
// Primary Key
this.HasKey(t => new { t.A, t.B}); // This is the problem
// Relationships
this.HasRequired(t => t.A)
.WithOptional(t => t.C);
// cannot use .HasForeignKey
this.HasRequired(t => t.B)
.WithOptional(t => t.C);
// cannot use .HasForeignKey
}
}
HasKeyt=>new{t.A,t.B}是不允许的,它需要一个标量属性,该属性通常使用.HasForeignKey设置。然而,由于这是一对一,我不能使用它
怎么办?当指定A->C.WithOptional时,EF假设表A和表C具有相同的PK
我建议记住0..1/注释并指定.WithMany.HasForeignKey。。在A->C和B->C链接中。我似乎试图做一些EF6不支持的事情。我也在想类似的事情。