在实体框架 7 中指定 ON DELETE NO ACTION? [英] Specifying ON DELETE NO ACTION in Entity Framework 7?

查看:15
本文介绍了在实体框架 7 中指定 ON DELETE NO ACTION?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Entity Framework 7 中,当我尝试应用迁移时出现错误

In Entity Framework 7 when I am trying to apply a migration I get the error

在表ChangeOrder"上引入 FOREIGN KEY 约束FK_ChangeOrder_User_CreatedByID"可能会导致循环或多个级联路径.指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束.
无法创建约束.查看以前的错误.

Introducing FOREIGN KEY constraint 'FK_ChangeOrder_User_CreatedByID' on table 'ChangeOrder' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

我知道在旧版本的实体框架中你会通过添加来处理这个

I know in older versions of Entity Framework you would deal with this by adding

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

到 DbContext 但在 EF7 modelBuilder 似乎没有 .Conventions 到它,谷歌只返回旧的 EF 4 虽然 EF 6 结果.

to the DbContext but in EF7 modelBuilder does not seem to have a .Conventions to it and google is only returning older EF 4 though EF 6 results.

如何在 Entity Framework 7 中指定 ON DELETE NO ACTION 约束?

How do I specific the ON DELETE NO ACTION constraint in Entity Framework 7?

Oleg 提供的答案显然会按外键执行,但我想在全局范围内执行此操作,因为使用一行代码在全局范围内声明这一点会容易得多,然后必须为数百个中的每一个指定代码我最终会拥有的关系.

The answer provided by Oleg will apparently do it per Foreign Key but I would like to do it globally as it will much easier to use one line of code to declare this globally then have to specify code it out for every single one of the hundreds of relationships I will end up having.

编辑 2:Oleg 的代码

Edit 2: Code for Oleg

public class ChangeOrder
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public Int16? ApprovedByID { get; set; }
    public Byte ApprovalStatusID { get; set; }
    public Int16 AssignedToID { get; set; }
    public Int16 CreatedByID { get; set; }
    public Byte CurrentStatusID { get; set; }
    public DateTime? DateApproved { get; set; }
    public DateTime? EndDate { get; set; }
    public Byte ImpactID { get; set; }
    public Byte PriorityID { get; set; }
    public DateTime? StartDate { get; set; }
    public Byte TypeID { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string ReasonForChange { get; set; }

    [ForeignKey("ApprovedByID")]
    public User ApprovedBy { get; set; }

    [ForeignKey("ApprovalStatusID")]
    public ChangeApprovalStatus ApprovalStatus { get; set; }

    [ForeignKey("AssignedToID")]
    public User AssignedTo { get; set; }

    [ForeignKey("CreatedByID")]
    public User CreatedBy { get; set; }

    [ForeignKey("ImpactID")]
    public ChangeImpact Impact { get; set; }

    [ForeignKey("PriorityID")]
    public ChangePriority Priority { get; set; }

    [ForeignKey("TypeID")]
    public ChangeType ChangeType { get; set; }

    [ForeignKey("CurrentStatusID")]
    public ChangeStatus CurrentStatus { get; set; }
}

public class JobSightDBContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelbuilder)
    {
        base.OnModelCreating(modelbuilder);
    }

    DbSet<ChangeApprovalStatus> ChangeApprovalStatus { get; set; }
    DbSet<ChangeImpact> ChangeImapct { get; set; }
    DbSet<ChangeOrder> ChangeOrders { get; set; }
    DbSet<ChangePriority> ChangePriorities { get; set; }
    DbSet<ChangeStatus> ChangeStatus { get; set; }
    DbSet<ChangeType> ChangeTypes { get; set; }
    DbSet<User> Users { get; set; }
}

推荐答案

在 GitHub 上挖掘并与那里一位非常有耐心的 MS 人员合作后,当前的解决方案是将其添加到 DbContext

After digging around on GitHub, and working with a very patient guy from MS there, the current solution is to add this to the DbContext

protected override void OnModelCreating(ModelBuilder modelbuilder)
{
    foreach (var relationship in modelbuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
    {
        relationship.DeleteBehavior = DeleteBehavior.Restrict;
    }

    base.OnModelCreating(modelbuilder);
}

这篇关于在实体框架 7 中指定 ON DELETE NO ACTION?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆