2012-08-02 171 views
1

在代碼優先的EF項目上有一個奇怪的問題。實體框架多對多級聯刪除問題

我有以下實體:

public class Booking 
{ 
    public Guid BookingId { get; set; } 
    public virtual List<AccountingDocumentItem> AccountingDocumentItems { get; set; } 
} 

public class AccountingDocumentItem 
{ 
    public Guid AccountingDocumentItemId { get; set; } 

    public virtual List<Employee> Employees { get; set; } 
    public virtual List<Booking> Bookings { get; set; } 
} 

public class Employee 
{ 
    public Guid EmployeeId { get; set; } 
    public string Name { get; set; } 

    public virtual List<AccountingDocumentItem> AccountingDocumentItems { get; set; } 
} 

正如你所看到的,還有,就是要AccountingDocumentItem無一不預訂和員工之間的許多一對多的關係。在配置我的AccountingDocumentItem時,我使用以下內容:

public AccountingDocumentItemConfiguration() 
    { 
     HasMany(x => x.Employees); 
     HasMany(x => x.Bookings); 
    } 

奇怪的是,這對於員工來說是完美的。我創建了一個AccountingDocumentItemEmployees表。但對於預訂我得到以下錯誤:

「對錶‘AccountingDocumentItemBookings’可能會導致循環或多個級聯路徑引進國外KEY約束‘FK_dbo.AccountingDocumentItemBookings_dbo.Bookings_Booking_BookingId’指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或。修改其他FOREIGN KEY約束。「

現在我試圖沿着下面的代碼行做到這一點:

HasMany(x => x.Bookings).WithMany(b => b.AccountingDocumentItems)... 

但我只得到選項使用上述線做一個地圖,沒辦法做一個WillCascadeOnDelete(假) 。

有人可以指出我做錯了什麼,因爲比較它與我如何處理員工我看不出任何區別。

編輯:

我原來的職位縮寫的實體,這可能是問題的根源所在引起的。以下是完整的實體:

public class AccountingDocument 
{ 
    public Guid AccountingDocumentId { get; set; } 
    public Guid SiteId { get; set; } 
    public virtual Site Site { get; set; } 
    public Guid? ClientId { get; set; } 
    public virtual Client Client { get; set; } 
    public Guid? SupplierId { get; set; } 
    public virtual Supplier Supplier { get; set; } 
    public string DocumentNumber { get; set; } 
    public string Reference { get; set; } 
    public string Description { get; set; } 
    public string Notes { get; set; } 
    public Guid LinkedAccountingDocumentId { get; set; } 
    public virtual AccountingDocument LinkedAccountingDocument { get; set; } 
    public byte AccountingDocumentTypeId { get; set; } 
    public DateTime CreationDate { get; set; } 
    public DateTime DocumentDate { get; set; } 
    public decimal Total { get; set; } 
    public Guid UserId { get; set; } 
    public virtual User User { get; set; } 
    public string Room { get; set; } 
    public virtual List<AccountingDocumentItem> AccountingDocumentItems { get; set; } 
} 

public class AccountingDocumentItem 
{ 
    public Guid AccountingDocumentItemId { get; set; } 

    public Guid AccountingDocumentId { get; set; } 
    public virtual AccountingDocument AccountingDocument { get; set; } 

    public string Description { get; set; } 

    public Guid TaxId { get; set; } 
    public virtual Tax Tax { get; set; } 

    public decimal Quantity { get; set; } 
    public string Unit { get; set; } 

    public decimal Cost { get; set; } 
    public decimal SellInclusive { get; set; } 
    public decimal SellExclusive { get; set; } 
    public decimal DiscountPercentage { get; set; } 
    public decimal TotalInclusive { get; set; } 
    public decimal TotalExclusive { get; set; } 
    public decimal CommissionInclusive { get; set; } 
    public decimal CommissionExclusive { get; set; } 
    public int LoyaltyPoints { get; set; } 
    public bool IsSeries { get; set; } 

    public byte ItemType { get; set; } 

    public Guid? ServiceId { get; set; } 
    public virtual Service Service { get; set; } 
    public Guid? ProductId { get; set; } 
    public virtual Product Product { get; set; } 
    public Guid? VoucherId { get; set; } 
    public virtual Voucher Voucher { get; set; } 

    public int SortOrder { get; set; } 

    public Guid? SourceId { get; set; } 
    public virtual Source Source { get; set; } 

    public Guid? CostCentreId { get; set; } 
    public virtual CostCentre CostCentre { get; set; } 

    public Guid? ClientId { get; set; } 
    public virtual Client Client { get; set; } 

    public Guid PackageGroupId { get; set; } 
    public Guid PackageServiceId { get; set; } 

    public virtual List<Employee> Employees { get; set; } 
    public virtual List<Booking> Bookings { get; set; } 
    public virtual List<MedicalDiagnosis> MedicalDiagnoses { get; set; } 
} 

public class Booking 
{ 
    public Guid BookingId { get; set; } 

    public Guid SiteId { get; set; } 
    public Site Site { get; set; } 

    public Guid? ClientId { get; set; } 
    public Client Client { get; set; } 

    public Guid BookingStateId { get; set; } 
    public BookingState BookingState { get; set; } 

    public virtual List<AccountingDocumentItem> AccountingDocumentItems { get; set; } 
} 

而且我的配置:

public class AccountingDocumentConfiguration : EntityTypeConfiguration<AccountingDocument> 
{ 
    public AccountingDocumentConfiguration() 
    { 
     Property(x => x.Reference).HasMaxLength(200); 
     HasRequired(x => x.Site); 
     Property(x => x.DocumentNumber).IsRequired().HasMaxLength(100); 
     Property(x => x.Reference).HasMaxLength(200); 
     Property(x => x.Description).HasMaxLength(500); 
     Property(x => x.Notes).HasMaxLength(500); 
     HasOptional(x => x.LinkedAccountingDocument); 
     Property(x => x.AccountingDocumentTypeId).IsRequired(); 
     Property(x => x.CreationDate).IsRequired(); 
     Property(x => x.DocumentDate).IsRequired(); 
     Property(x => x.Total).IsRequired(); 
     Property(x => x.Room).HasMaxLength(50); 
    } 
} 

public class AccountingDocumentItemConfiguration : EntityTypeConfiguration<AccountingDocumentItem> 
{ 
    public AccountingDocumentItemConfiguration() 
    { 
     Property(x => x.Description).IsRequired().HasMaxLength(200); 
     HasMany(x => x.Employees); 
     HasMany(x => x.Bookings); 
     HasMany(x => x.MedicalDiagnoses); 
     Property(x => x.Unit).HasMaxLength(50); 
    } 
} 
+0

我完全按照上面的方法複製了你的代碼,它對我有效,你縮寫了嗎?如果是這樣,你可以發佈你的模型的其餘部分? – 2012-08-03 01:45:50

+0

嗨馬克,是的,我縮短了實體,我已經用完整的實體進行了編輯。 – Gary 2012-08-03 07:39:26

回答

2

即使與它上面所添加的文本的工作對我來說,一旦我註釋掉未完全上面定義的添加導航性能。 FK錯誤意味着有可能是一個競爭條件,如果你碰巧刪除(見這article),但與什麼,我不能告訴。你需要在數據庫上進行級聯刪除嗎?如果沒有,你可以把它關掉 - 我意識到這是一個非常寬泛的中風,但一個小問題。

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); 

如果這不是一個選項 - 這是別的東西,你沒有包括在內。你有預訂表的映射嗎?它看起來像預訂有一個必需的網站 - 是否有可能刪除一個網站,可能會觸發刪除一堆其他的東西?它看起來像網站可以做這樣的網站 - >帳戶文件 - >會計文件項目..網站 - >預訂可能嗎?

這是另一個可能相關的SO question