我对该级联的工作方式有些困惑。所以我将这两个实体链接在一起。
销售合同:
public class SalesContract : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
[Index(IsUnique = true)]
[StringLength(50)]
public string SCNo { get; set; }
[Required]
public string DeliveryAddress { get; set; }
public DateTime DeliveryDate { get; set; } = DateTime.UtcNow;
[Required]
public string Transport { get; set; }
[ForeignKey("PaymentMethod")]
public Guid PaymentMethodId { get; set; }
public virtual PaymentMethod PaymentMethod { get; set; }
public int PaymentTime { get; set; }
public string Note { get; set; }
[ForeignKey("ItemType")]
public Guid ItemTypeId { get; set; }
public ItemType ItemType { get; set; }
public Guid SalesId { get; set; }
public virtual Account Sales { get; set; }
[ForeignKey("Buyer")]
public Guid BuyerId { get; set; }
public virtual Buyer Buyer { get; set; }
[Required]
public string Status { get; set; } = "Pending"; //Pending / Approved
public Guid? ApprovedById { get; set; }
public virtual Account ApprovedBy { get; set; }
public bool? IsApproved { get; set; }
[AutoGenerated]
public DateTime DateCreated { get; set; } = DateTime.UtcNow;
public ICollection<SalesContractItem> Items { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
this.Items.ForEach(m => { db.SalesContractItems.Remove(m); });
db.SalesContracts.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
和销售合同项目:
public class SalesContractItem : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[ForeignKey("Item")]
public Guid ItemId { get; set; }
public virtual Item Item { get; set; }
[ForeignKey("Color")]
public Guid ColorId { get; set; }
public virtual Color Color { get; set; }
[ForeignKey("Unit")]
public Guid UnitId { get; set; }
public virtual Unit Unit { get; set; }
[ForeignKey("ItemType")]
public Guid ItemTypeId { get; set; }
public virtual ItemType ItemType { get; set; }
public decimal Price { get; set; }
[ForeignKey("Currency")]
public Guid CurrencyId { get; set; }
public Currency Currency { get; set; }
[Required]
public decimal BaseQty { get; set; }
public decimal Remaining { get; set; }
public string Description { get; set; }
[AutoGenerated]
public DateTime DateCreated { get; set; } = DateTime.UtcNow;
[ForeignKey("SalesContract")]
public Guid SalesContractId { get; set; }
public virtual SalesContract SalesContract { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.SalesContractItems.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
ObjectMapper类仅包含函数,没有实体属性。
完整的错误消息是这样的:
在表'SalesContractItems'上引入FOREIGN KEY约束'FK_dbo.SalesContractItems_dbo.SalesContracts_SalesContractId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 无法创建约束或索引。查看先前的错误。
这两个实体在删除时会导致多个级联是怎么回事?
根据要求,这些是链接到两者的完整实体。
public class Item : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
[Index(IsUnique = true)]
[StringLength(50)]
public string ItemName { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.Items.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
public class Color : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
[Index(IsUnique = true)]
[StringLength(50)]
public string ColorCode { get; set; }
public string ColorName { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.Colors.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
public class Unit : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
[Index(IsUnique = true)]
[StringLength(50)]
public string UnitName { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.Units.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
public class ItemType : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
[Index(IsUnique = true)]
[StringLength(50)]
public string ItemTypeName { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.ItemTypes.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
public class PaymentMethod : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
public string PaymentMethodName { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.PaymentMethods.Remove(this);
await db.SaveChangesAsync();
return true;
}
}
public class Currency : ObjectMapper
{
[Key]
[Index(IsUnique = true)]
[AutoGenerated]
public Guid ID { get; set; } = Guid.NewGuid();
[Required]
[Index(IsUnique = true)]
[StringLength(50)]
public string CurrencyName { get; set; }
[Required]
public string CurrencySymbol { get; set; }
public async Task<bool> Delete(MigrationDBContext db)
{
db.Currencies.Remove(this);
await db.SaveChangesAsync();
return true;
}
}