简体   繁体   English

配置级联删除

[英]Configuring cascade delete

I have 8 objects which reference a generic entity called ExpenseType. 我有8个对象引用了称为ExpenseType的通用实体。 There are 36 references to ExpenseType between the 8 objects. 8个对象之间有36个对ExpenseType的引用。 I'd like to configure cascade delete so that when the referencing objects are deleted, so too are the ExpenseType records. 我想配置级联删除,以便在删除引用对象时,ExpenseType记录也被删除。

I'm using Code First Entity Framework 5 and have already done this for other objects in my schema, but the generic nature of this has me stumped. 我正在使用Code First Entity Framework 5,并且已经对架构中的其他对象执行了此操作,但是这种通用性使我感到困惑。

Any and all help is greatly appreciated. 任何帮助都将不胜感激。

Thanks John 谢谢约翰

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; }
}

Override OnModelCreating and try this. 重写OnModelCreating并尝试此操作。 Unless I am forgetting something I think it should work. 除非我忘记了一些东西,否则我认为它应该起作用。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM