简体   繁体   English

代码优先级联删除,不使用Fluent API

[英]Code First Cascade Delete Not Using Fluent API

I have these two entities 我有这两个实体

class AUT
{
    public Guid ID { get; set; }
    public string Name { get; set; }

    public Engineer Engineer { get; set; }
}

class InstallationSetup
{
    public virtual AUT ApplicationUnderTesting { get; set; }

    public Guid ID { get; set; }
    // Loads of properties etc
}

class Engineer
{
    public Guid ID { get; set; }
    public string Name { get; set; }
}

Using code first and some data annotations these entities create a database. 这些实体首先使用代码和一些数据注释来创建数据库。 I'm using EF 5, When I delete an Application, it should only delete itself and any InstallationSetup that has been referenced to it. 我正在使用EF 5,当我删除应用程序时,它应该只删除自身和已引用到它的所有InstallationSetup。 It shouldn't delete the Engineer. 它不应该删除工程师。 However when I do try and delete it, I get the error: 但是,当我尝试删除它时,出现错误:

The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.InstallationSetups_dbo.AUTs_ApplicationUnderTesting_ID". DELETE语句与REFERENCE约束“ FK_dbo.InstallationSetups_dbo.AUTs_ApplicationUnderTesting_ID”冲突。 The conflict occurred in database "UXLab", table "dbo.InstallationSetups", column 'ApplicationUnderTesting_ID'. 在数据库“ UXLab”的表“ dbo.InstallationSetups”的列“ ApplicationUnderTesting_ID”中发生了冲突。 The statement has been terminated. 该语句已终止。

So, I'm guessing that because there is another table with an entry relying on the AUT to be there, by deleting the AUT you will leave InstallationSetup with null foreign key thus a broken row. 因此,我猜测是因为存在另一个表,该表具有依赖于AUT的条目,所以通过删除AUT,您将留下具有空外键的InstallationSetup,从而导致行损坏。

I should be able to (preferably not using Fluent API) tell entity framework that any thing that has a reference to AUT should also be deleted? 我应该能够(最好不使用Fluent API)告诉实体框架任何具有对AUT引用的内容也应该删除? This is what I want to achieve. 这是我想要实现的。

you just have to add a column that is similar as your generated Foreign key column, when entity framework generates this FK column it set cascading delete to disabled. 您只需要添加与生成的外键列相似的列即可,当实体框架生成此FK列时,它将级联删除设置为禁用。

class AUT
{
   public Guid ID { get; set; }
   public string Name { get; set; }

  public Engineer Engineer { get; set; }
}

class InstallationSetup
{
    public virtual AUT ApplicationUnderTesting { get; set; }
    public int ApplicationUnderTestingId {get; set;}   <--- Add this.

   public Guid ID { get; set; }
// Loads of properties etc
}

class Engineer
{
 public Guid ID { get; set; }
 public string Name { get; set; }
}

If you generate your database again, you see that some things are changed. 如果再次生成数据库,则会看到某些更改。 The automatically generated column AUTs_ApplicationUnderTesting_ID is no longer there and the ApplicationUnderTestingId column is now used for your foreign key relationship. 自动生成的列AUTs_ApplicationUnderTesting_ID不再存在,并且ApplicationUnderTestingId列现在用于您的外键关系。

EF will enable cascading delete automatically now. EF现在将自动启用级联删除。

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

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