简体   繁体   English

与Entity Framework ASP.net MVC中的多个表的可选关系

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

I'm trying to build a ASP.net MVC application. 我正在尝试构建一个ASP.net MVC应用程序。 I'm having trouble making some kind of relation with data annotations. 我在与数据注释建立某种联系时遇到麻烦。

I have 3 tables, Overhours , Accountings , Vacations . 我有3个表,“ Overhours ,“ Accountings ,“ Vacations Every Overhour record can have 1 Accounting or Vacation record but it's optional. 每个“ Overhour记录可以有1个“ Accounting或“ Vacation记录,但这是可选的。 So, it doesn't need to have to have one. 因此,它不必必须拥有一个。 Here is my Overhour model: 这是我的Overhour模式:

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). 我想删除“ Overhour记录(如果有的话)时同时删除“ Vacation和“ Accounting记录。 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. 级联删除有效,但是Entity Framework创建诸如“ Accounting_AccountingId”之类的字段,这也是必需的。 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. 在表xxx上引入FOREIGN KEY xxx约束可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束

I'm pretty confused, what am i doing wrong? 我很困惑,我在做什么错?

Thanks 谢谢

DELETE CASCADE doesn't work like that. DELETE CASCADE不能那样工作。 If you delete Overhour , nothing else will nor should be deleted. 如果您删除Overhour ,则也不会删除任何其他内容。 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. 如果Accounting是必需的依赖项,并且您删除了附加到OverhourAccounting实例,则Overhour应该删除Overhour That won't occur if the foreign key is nullable, as then it will be set to DELETE SET NULL, instead. 如果外键可为空,则不会发生这种情况,因为它将被设置为DELETE SET NULL。 Either way, though, deleting Overhour will never have any effect on Accounting or Vacation instances. 但是,无论哪种方式,删除“ Overhour都不会对“ Accounting或“ Vacation实例产生任何影响。

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 . 这样,删除Overhour也会级联并删除关联的Accounting ,因为它取决于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. 然后,您必须使用fluent config将一个设置为主体,将一个设置为从属,因为EF当时无法自行做出此决定。

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

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