正在配置级联删除

本文关键字:删除 级联 配置 | 更新日期: 2023-09-27 18:28:09

我有8个对象,它们引用了一个名为ExpenseType的通用实体。在这8个对象之间有36个对ExpenseType的引用。我想配置级联删除,这样当引用对象被删除时,ExpenseType记录也被删除。

我使用的是代码优先实体框架5,并且已经为我的模式中的其他对象做了这件事,但这件事的通用性让我很困惑。

我们非常感谢所有的帮助。

谢谢约翰·

public class HouseholdExpenses
    {
        [Key]
        public virtual int HouseholdExpensesId { get; set; }
        [Display(Name = "Childcare")]
        public virtual ExpenseType Childcare { get; set; }
        [Display(Name = "Elderly Care (Carer Nursing Home etc)")]
        public virtual ExpenseType ElderlyCare { get; set; }
        [Display(Name = "Food / Housekeeping / Personal Care")]
        public virtual ExpenseType FoodHousekeeping { get; set; }
        [Display(Name = "Clothing and Footwear")]
        public virtual ExpenseType ClothingFootwear { get; set; }
        [Display(Name = "House Repairs and Maintenance")]
        public virtual ExpenseType RepairsAndMaintenance { get; set; }
}
    public class MedicalExpenses
    {
        [Key]
        public virtual int MedicalExpensesId { get; set; }
        [Display(Name = "Medical Expenses and Prescription Charges")]
        public virtual ExpenseType MedicalExpensesAndPrescriptions { get; set; }
        [Display(Name = "Health Insurance (unless deducted from your salary at source)")]
        public virtual ExpenseType HealthInsurance { get; set; }
}
public class ExpenseType
{
    [Key]
    public virtual int ExpenseTypeId { get; set; }
    [Display(Name = "Amount")]
    public virtual decimal? Amount { get; set; }
    [Display(Name = "Arrears")]
    public virtual decimal? Arrears { get; set; }
}

正在配置级联删除

覆盖OnModelCreating并尝试此操作。除非我忘记了什么,否则我认为它应该起作用。

mBuilder.Entity<ExpenseType>()
            .HasRequired(he => he.Childcare)
            .WithMany();
mBuilder.Entity<ExpenseType>()
            .HasRequired(ec => ec.ElderlyCare)
            .WithMany();
...