简体   繁体   中英

Optional relationship to more than one table in Entity Framework ASP.net MVC

I'm trying to build a ASP.net MVC application. I'm having trouble making some kind of relation with data annotations.

I have 3 tables, Overhours , Accountings , Vacations . Every Overhour record can have 1 Accounting or Vacation record but it's optional. So, it doesn't need to have to have one. Here is my Overhour model:

public class Overhour
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OverhourId { get; set; }

    ....

    public int? AccountingId { get; set; }
    public virtual Accounting Accounting { get; set; }

    public int? VacationId { get; set; }
    public virtual Vacation Vacation { get; set; }
}

I want to have both Vacation and Accounting record deleted when i delete my Overhour record (if there is any). When i use it like this, cascading delete gets disabled.

I tried this:

public class Overhour
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OverhourId { get; set; }

    ....

    public virtual Accounting Accounting { get; set; }

    public virtual Vacation Vacation { get; set; }
}

Cascading delete works but Entity Framework creates fields like "Accounting_AccountingId", also it becomes required. Those shouldn't be required.

Last thing i tried was this:

public class Overhour
{

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OverhourId { get; set; }

    ....

    public int AccountingId { get; set; }
    public virtual Accounting Accounting { get; set; }

    public int VacationId { get; set; }
    public virtual Vacation Vacation { get; set; }
}

But this time it gives me an error like this:

Introducing FOREIGN KEY xxx constraint on table xxx may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints

I'm pretty confused, what am i doing wrong?

Thanks

DELETE CASCADE doesn't work like that. If you delete Overhour , nothing else will nor should be deleted. It works in the opposite direction. If Accounting is a required dependent, and you delete the Accounting instance that's attached to Overhour , then Overhour should also, then, be deleted. That won't occur if the foreign key is nullable, as then it will be set to DELETE SET NULL, instead. Either way, though, deleting Overhour will never have any effect on Accounting or Vacation instances.

UPDATE

Creating a one-to-one, which it seems like you need here, is relatively easy in this scenario, since one side of the relationship is optional. All you need is:

public class Overhour
{
    ...

    public int? AccountingId { get; set; } // optional
    public virtual Accounting Accounting { get; set; }
}

public class Accounting
{
    ...

    public int OverhourId { get; set; } // required
    public virtual Overhour Overhour { get; set; }
}

With that, deleting the Overhour will cascade and delete the associated Accounting as well, since it's dependent on Overhour .

Things only get complicated when both sides of the relationship are required. Then, you must use fluent config to set one as the principal and one as the dependent, since EF can't make this decision on its own at that point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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