1
使用EF5,我想一個一對多的映射Car:Wheel == 1:0..n
一到多的使用EF5流利的API
public class Car {
public int ID { get; set; }
public virtual ICollection<Wheel> Wheels { get; set; }
}
public class Wheel {
public int ID { get; set; }
// I don't want to define a reverse relationship here
}
所以對於Car
我所做的:
modelBuilder.Entity<Car>()
.HasMany(x => x.Wheels)
.WithMany()
.Map(x => x
.MapLeftKey("CarID")
.MapRightKey("WheelID")
.ToTable("Car_Wheel"));
這給了我一個n:n
連接表。但我想要1:n
。
我需要在Car_Wheel.CarID
上定義一個唯一約束(如果是這樣,怎麼做?),還是有更簡單的方法?
謝謝。但不幸的是,這會在Children表中創建一個FK。出於設計原因,我不需要這樣的關鍵。然而,這將是正確的做法。 – 2013-04-29 14:01:44
第一種情況(使用'Map')使用獨立的關聯(因此它不使用任何外鍵)。 – Dennis 2013-04-29 14:04:18
剛剛嘗試過,它會在Children表中創建一個FK。如果你省略'.Map()',那麼它只是按照慣例命名該鍵,但它在那裏。我不能擁有那把鑰匙。我認爲連接表是不可避免的,但我不確定如何欺騙它表現爲1:1。 – 2013-04-29 14:07:31