Dot-Net
EF 6 - 一對多的級聯刪除,沒有反向引用
我有這樣的事情:
public class Gadget { public int Id { get; set; } public string Name { get; set;} public int SuperHeroId { get; set; } } public class SuperHero { public int Id { get; set; } public virtual ICollection<Gadget> Gadgets { get; set; } }請注意,雖然小工具由超級英雄“擁有”(因此數據庫中有一個 FK),但我的域模型在該方向上沒有硬引用。
當我刪除一個超級英雄時,我也想刪除他們所有的小工具。我該怎麼做?
我的研究表明,如果我有那個參考,它會像
mapping.Entity<SuperHero>() .HasMany(x => x.Gadgets) .WithRequired(x => x.SuperHero) //this is the part I can't do .WillCascadeOnDelete();但如前所述,這不適用於我的域模型。
mapping.Entity<SuperHero>() .HasMany(x => x.Gadgets) .WithRequired() //use the override that doesn't //specify a navigation property .WillCascadeOnDelete();http://msdn.microsoft.com/en-us/library/gg696502(v=vs.113).aspx
將關係配置為可選:在關係的另一端沒有導航屬性的情況下是必需的。