2016-10-10 10 views
1

は、私はFluentApiを使用して指定するテーブルA、B.IdとC.Id中のPaireはそれだけでナビゲーションプロパティを持つことはできませんユニーク複合一意インデックス

class A 
{ 
    public int Id { get; set; } 
    public B B { get; set; } 
    public C C { get; set; } 
} 
class B 
{ 
    public int Id { get; set; } 
} 
class C 
{ 
    public int Id { get; set; } 
} 
+0

BとCの組み合わせがユニークである場合は、本当にあなたを行うに

あなたは、明示的なFKフィールドを追加する必要がありますIDが必要ですか? – Win

答えて

1

あること。

class A 
{ 
    public int Id { get; set; } 
    public int B_Id { get; set; } 
    public int C_Id { get; set; } 
    public B B { get; set; } 
    public C C { get; set; } 
} 

と、次の流暢APIの設定使用して一意のインデックスを作成します:

modelBuilder.Entity<A>().HasRequired(e => e.B).WithMany().HasForeignKey(e => e.B_Id); 
modelBuilder.Entity<A>().HasRequired(e => e.C).WithMany().HasForeignKey(e => e.C_Id); 

modelBuilder.Entity<A>().Property(e => e.B_Id).HasColumnAnnotation("Index", 
    new IndexAnnotation(new IndexAttribute("IX_BC", 1) { IsUnique = true })); 
modelBuilder.Entity<A>().Property(e => e.C_Id).HasColumnAnnotation("Index", 
    new IndexAnnotation(new IndexAttribute("IX_BC", 2) { IsUnique = true })); 
関連する問題