Entity framework 4 实体框架多对多级联删除问题

Entity framework 4 实体框架多对多级联删除问题,entity-framework-4,ef-code-first,many-to-many,code-first,Entity Framework 4,Ef Code First,Many To Many,Code First,在代码优先的EF项目中遇到了一个奇怪的问题 我拥有以下实体: public class Booking { public Guid BookingId { get; set; } public virtual List<AccountingDocumentItem> AccountingDocumentItems { get; set; } } public class AccountingDocumentItem { public Guid Account

在代码优先的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; }
}
奇怪的是,这对员工来说非常有效。我创建了AccountingDocumentItemEmployees表。但是对于预订,我得到以下错误:

在表“AccountingDocumentItemBookings”上引入外键约束“FK_dbo.AccountingDocumentItemBookings_dbo.Bookings_Bookings_BookingId”可能会导致循环或多个级联路径。请在“删除不操作”或“更新不操作”时指定,或修改其他外键约束

现在,我已尝试按照以下代码行执行此操作:

 HasMany(x => x.Bookings).WithMany(b => b.AccountingDocumentItems)...
但是我只能选择使用上面的行进行映射,没有选择执行WillCascadeOnDelete(false)

有人能指出我做错了什么吗?因为与我处理员工的方式相比,我看不出有什么不同

编辑:

我最初的帖子缩写了实体,这可能就是问题所在。以下是完整的实体:

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; }
}
公共类会计凭证
{
公共Guid AccountingDocumentId{get;set;}
公共Guid站点ID{get;set;}
公共虚拟站点站点{get;set;}
公共Guid?ClientId{get;set;}
公共虚拟客户端{get;set;}
公共Guid?供应商ID{get;set;}
公共虚拟供应商{get;set;}
公共字符串DocumentNumber{get;set;}
公共字符串引用{get;set;}
公共字符串说明{get;set;}
公共字符串注释{get;set;}
公共Guid LinkedAccountingDocumentId{get;set;}
公共虚拟帐户文档LinkedAccountingDocument{get;set;}
公共字节AccountingDocumentTypeId{get;set;}
公共日期时间创建日期{get;set;}
公共日期时间文档日期{get;set;}
公共十进制总数{get;set;}
公共Guid用户标识{get;set;}
公共虚拟用户用户{get;set;}
公共字符串室{get;set;}
公共虚拟列表AccountingDocumentItems{get;set;}
}
公共类AccountingDocumentItem
{
公共Guid AccountingDocumentItemId{get;set;}
公共Guid AccountingDocumentId{get;set;}
公共虚拟帐户文档帐户文档{get;set;}
公共字符串说明{get;set;}
公共Guid TaxId{get;set;}
公共虚拟税{get;set;}
公共十进制数量{get;set;}
公共字符串单元{get;set;}
公共十进制成本{get;set;}
公共十进制{get;set;}
公共十进制SellExclusive{get;set;}
公共十进制折扣百分比{get;set;}
公共十进制全包{get;set;}
公共十进制TotalExclusive{get;set;}
公共十进制委员会结论{get;set;}
公共十进制委员{get;set;}
public int-LoyaltyPoints{get;set;}
公共布尔IsSeries{get;set;}
公共字节ItemType{get;set;}
公共Guid?ServiceId{get;set;}
公共虚拟服务服务{get;set;}
公共Guid?ProductId{get;set;}
公共虚拟产品产品{get;set;}
公共Guid?VoucherId{get;set;}
公共虚拟凭证凭证{get;set;}
公共int排序器{get;set;}
公共Guid?源ID{get;set;}
公共虚拟源{get;set;}
公共Guid?CostCentreId{get;set;}
公共虚拟成本中心成本中心{get;set;}
公共Guid?ClientId{get;set;}
公共虚拟客户端{get;set;}
公共Guid PackageGroupId{get;set;}
公共Guid PackageServiceId{get;set;}
公共虚拟列表雇员{get;set;}
公共虚拟列表预订{get;set;}
公共虚拟列表MedicalDiagnoses{get;set;}
}
公共课预订
{
公共Guid BookingId{get;set;}
公共Guid站点ID{get;set;}
公共站点站点{get;set;}
公共Guid?ClientId{get;set;}
公共客户端{get;set;}
公共Guid BookingStateId{get;set;}
公共BookingState BookingState{get;set;}
公共虚拟列表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);
    }
}
公共类AccountingDocumentConfiguration:EntityTypeConfiguration
{
公共帐户文档配置()
{
属性(x=>x.Reference).HasMaxLength(200);
需要(x=>x.Site);
属性(x=>x.DocumentNumber).IsRequired().HasMaxLength(100);
属性(x=>x.Reference).HasMaxLength(200);
属性(x=>x.Description).HasMaxLength(500);
属性(x=>x.Notes);
has可选(x=>x.LinkedAccountingDocument);
属性(x=>x.AccountingDocumentTypeId).IsRequired();
属性(x=>x.CreationDate).IsRequired();
属性(x=>x.DocumentDate).IsRequired();
属性(x=>x.Total).IsRequired();
属性(x=>x.Room).HasMaxLength(50);
}
}
公共类AccountingDocumentItemConfiguration:EntityTypeConfiguration
{
公共帐户DocumentItemConfiguration()
{
属性(x=>x.Description).IsRequired().HasMaxLength(200);
拥有许多员工(x=>x名员工);
有很多(x=>x.预订);
有许多(x=>x.MedicalDiagnoses);
属性(x=>x.Unit).HasMaxLength(50);
}
}

即使添加了上面的文本,只要我注释掉上面没有完全定义的添加的导航属性,它也对我有效。FK错误意味着,如果您碰巧删除(请参见此),可能会出现竞争条件,但对于此处的内容,我无法确定。您需要在数据库上进行级联删除吗?如果没有,你可以把它关掉——不过我意识到这对一个小问题来说是一个很大的挑战

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();  
modelBuilder.Conventions.Remove();
如果这不是一个选项,那就是你没有包括的其他东西。是否有Bookings表的映射?看起来像B
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();