我有兩個類:實體框架許多一對多關係錯誤
public class Cluster
{
public int Id { get; set; }
public virtual ICollection<Blob> Blobs { get; set; }
}
public class Blob
{
public int Id { get; set; }
public virtual ICollection<Cluster> Clusters { get; set; }
}
public ClusterConfiguration()
{
this.HasKey(p => p.Id)
.HasRequired(p => p.Frame)
.WithMany(p => p.Clusters)
.HasForeignKey(p => p.FrameId)
.WillCascadeOnDelete(true)
;
this.HasMany(p => p.Blobs)
.WithMany(p => p.Clusters)
;
}
public BlobConfiguration()
{
this.HasKey(p => p.Id)
.HasRequired(p => p.Frame)
.WithMany(p => p.Blobs)
.HasForeignKey(p => p.FrameId)
.WillCascadeOnDelete(true)
;
this.HasMany(p => p.Clusters)
.WithMany(p => p.Blobs)
;
}
有在這些類其他表的引用,但我不認爲這是問題。錯誤是:
[{"Introducing FOREIGN KEY constraint 'FK_dbo.ClusterBlobs_dbo.Blob_Blob_Id' on table 'ClusterBlobs' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.\r\nCould not create constraint. See previous errors."}].
我不太知道如何告訴EF級聯刪除斑點,如果集羣被刪除,但如果斑點被刪除不刪除集羣。請指教。
更新:順便說一下使用EF5。
謝謝@Slauma。我爲自己編寫了一個ORM方法,並假定報告EF級別的錯誤而不是傳遞從數據庫生成的底層約束錯誤會很容易。'[爲鏈接表禁用級聯刪除是不可能的]。我假設這是一個數據庫級限制,而不是EF。另外,我應該想到級聯刪除不適用於規範化的多對多關係。只是到目前爲止,我的模型中這兩個對象的身份還沒有很好地定義。 –
順便說一句,因爲級聯刪除僅適用於一個路徑,所以爲其他路徑寫入一些手動刪除代碼最合適的地方是什麼? –
@RaheelKhan:是的,這是一個數據庫級(SQL Server)限制。對於其他數據庫,它可能毫無例外地工作。我相信EF不知道刪除路徑。最合適的地方將取決於你的架構。如果您有(非通用)存儲庫可能在'Frame'的Delete方法中。 – Slauma