简体   繁体   English

实体框架6中“更新数据库”之后发生错误

[英]Error after 'update-database' in Entity Framework 6

I'am working on WEB API 2 with Entity Framework 6 project and have problem after update-database. 我正在使用Entity Framework 6项目开发WEB API 2,并且在更新数据库后遇到问题。

Error: 错误:

Introducing FOREIGN KEY constraint 'FK_dbo.Rates_dbo.Users_Id_User' on table 'Rates' 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 or index. See previous errors.

Here's my Rate class: 这是我的费率类:

public class Rate
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id_Rate { get; set; }
        public int Id_User { get; set; }
        public int Id_Recipe { get; set; }
        public int Value_Rate { get; set; } //1-5

        public virtual User User { get; set; }
        public virtual Recipe Recipe { get; set; }
    }

and my User class: 和我的用户类:

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id_User { get; set; }
    public int Id_List_Products { get; set; }
    public int Id_List_Black_Products { get; set; }
    public string Login { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public Boolean Social_Account { get; set; } // 1 - social account, 0 - normal account
    public string URL_Avatar { get; set; } //URL of avatar thumbnail

    public virtual List_Products List_Products { get; set; }
    public virtual List_Black_Products List_Black_Products { get; set; }
}

I have no idea where's problem. 我不知道哪里出了问题。 Any hints? 有什么提示吗?

This problem occurs due to multiple pathways of relations. 由于关系的多种途径而发生此问题。 If you delete one record, it will end up deleting other records from another table which will end up deleting more records from another table which in turn will end up deleting records from the table you started with and the cycle will repeat. 如果删除一个记录,它将最终从另一个表中删除其他记录,这将最终从另一个表中删除更多记录,这又将最终从您开始的表中删除记录,并且循环将重复。 Truly catastrophic, a developer's worst nightmare. 真正的灾难,是开发商最糟糕的噩梦。 To address this issue, add custom attributes, for instance, in your migration: 要解决此问题,例如,在迁移中添加自定义属性:

CreateTable(
            "dbo.SomeTable",
            c => new
            {
                id = c.Int(nullable: false, identity: true),
                createdon = c.DateTime(),
                createdby = c.String(),
            })
            .PrimaryKey(t => t.id)
            .ForeignKey("dbo.Customers", t => t.Customerid, cascadeDelete: true)
            .ForeignKey("dbo.AnotherTable", t => t.Anotherid, cascadeDelete: false)
            .Index(t => t.Customerid)
            .Index(t => t.Scopeid);

Set the cascadeDelete to false or AutoUpdate to false of the affected tables/(model classes backing) and update the database accordingly. 将受影响的表/(支持模型类)的cascadeDelete设置为false或将AutoUpdatefalse ,并相应地更新数据库。

Simply put, your relation is cyclic, for instance: Customer -> Salary -> Payroll -> Customer So when you delete a customer, its respective salary record(s) are/is deleted which delete the respected Payroll records which loop back to deleting linked customers and this cycle keeps repeating causing chaos. 简而言之,您的关系是周期性的,例如: 客户->薪水->工资单->客户因此,当您删除客户时,将删除其相应的薪水记录,从而删除受尊重的薪水记录,并循环回删除链接客户,并且此循环不断重复,造成混乱。 So Entity Framework and SQL understand this well and prompt the user to turn off affected/error-causing deletion/updation connections. 因此,Entity Framework和SQL很好地理解了这一点,并提示用户关闭受影响的/引起错误的删除/更新连接。

Okey, I changed cascadeDelete to false in: Okey,我在以下位置将cascadeDelete更改为false:

CreateTable(
            "dbo.Rates",
            c => new
                {
                    Id_Rate = c.Int(nullable: false, identity: true),
                    Id_User = c.Int(nullable: false),
                    Id_Recipe = c.Int(nullable: false),
                    Value_Rate = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.Id_Rate)
            .ForeignKey("dbo.Recipes", t => t.Id_Recipe, cascadeDelete: true)
            .ForeignKey("dbo.Users", t => t.Id_User, cascadeDelete: false)
            .Index(t => t.Id_User)
            .Index(t => t.Id_Recipe);

and it works ;) 它有效;)

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

相关问题 实体框架-迁移更新数据库引发奇怪的错误 - Entity Framework - Migrations update-database Throwing strange error 实体框架6更新数据库在卸载appdomain时引发错误 - Entity Framework 6 update-database throws error while unloading appdomain 实体框架核心 - “更新数据库”不起作用 - Entity Framework Core - 'Update-Database' not working 带有-ConnectionString的实体框架更新数据库 - Entity Framework Update-Database with -ConnectionString 实体框架代码第一次更新 - 数据库在CREATE DATABASE上失败 - Entity Framework code first update-database fails on CREATE DATABASE Entity Framework Core 不在更新数据库上创建数据库 - Entity Framework Core not creating database on update-database 实体框架代码优先:使用'Update-Database'生成SQL脚本在解析时会产生XML错误 - Entity Framework Code-First: Generate SQL script with 'Update-Database' produces XML error while parsing 更新数据库上的ASP.NET代码优先(实体框架)错误 - ASP.NET Code-First (Entity Framework) Error on update-database 使用Entity Framework,Code First(POCO)和Code Contracts运行Update-Database时出错 - Error When running Update-Database using Entity Framework, Code First (POCO) and Code Contracts 实体框架核心更新-数据库特定的迁移没有权限错误 - Entity Framework Core Update-database specific migration gives no permission error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM