【问题标题】:Error after 'update-database' in Entity Framework 6实体框架 6 中的“更新数据库”后出错
【发布时间】:2017-08-30 18:51:38
【问题描述】:

我正在使用 Entity Framework 6 项目开发 WEB API 2,更新数据库后出现问题。

错误:

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.

这是我的费率等级:

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; }
    }

和我的用户类:

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; }
}

我不知道哪里出了问题。 有什么提示吗?

【问题讨论】:

标签: c# asp.net entity-framework-6 asp.net-web-api2


【解决方案1】:

这个问题是由于关系的多种途径而发生的。如果您删除一条记录,它将最终从另一个表中删除其他记录,这将最终从另一个表中删除更多记录,这反过来将最终从您开始的表中删除记录,并且循环将重复。真正的灾难,是开发者最可怕的噩梦。要解决此问题,请添加自定义属性,例如,在您的迁移中:

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);

将受影响的表/(模型类支持)的cascadeDelete 设置为falseAutoUpdatefalse,并相应地更新数据库。

简单地说,你的关系是循环的,例如: 客户 -> 工资 -> 工资单 -> 客户 因此,当您删除客户时,其各自的工资记录将被删除,这会删除受人尊敬的工资记录,这些记录会循环回删除链接的客户,并且此循环不断重复导致混乱。所以 Entity Framework 和 SQL 很好地理解了这一点,并提示用户关闭受影响/导致错误的删除/更新连接。

【讨论】:

    【解决方案2】:

    好的,我将 cascadeDelete 更改为 false in:

    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);
    

    它有效;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2010-09-05
      • 2015-09-21
      • 2014-04-30
      • 1970-01-01
      • 2013-06-06
      相关资源
      最近更新 更多