2012-08-02 172 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