1
私は以下のクラスを持っています。しかし、データベースで生成されたテーブルで、私はAB
テーブルで見つかった:Entity Frameworkはデータベースに余分な外部キーを生成します
Id, AId, BId
。 ABMapping
クラスA
public class A
{
public int Id { get; set; }
ICollection<AB> ABs { get; set; }
}
クラスB
public class B
{
public int Id { get; set; }
ICollection<AB> ABs { get; set; }
}
AB級
public class AB
{
public int Id { get; set; }
public A A { get; set; }
public B B { get; set; }
public int AId { get; set; }
public int BId { get; set; }
}
public class ABMap : EntityTypeConfiguration<AB>
{
public ABMap()
{
this.HasKey(a => a.Id);
this.HasRequired(e => e.A)
.WithMany()
.HasForeignKey(e => e.AId)
.WillCascadeOnDelete(false);
this.HasRequired(c => c.B)
.WithMany()
.HasForeignKey(e => e.BId)
.WillCascadeOnDelete(false);
this.Property(e => e.AId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_AB", 1) { IsUnique = true }));
this.Property(e => e.BId)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("IX_AB", 2) { IsUnique = true }));
}
}
コンテキスト
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//...
modelBuilder.Configurations.Add(new ABMap());
//...
}