我正在使用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; }
}
我不知道问题出在哪里。 任何提示?
答案 0 :(得分:1)
由于多种关系路径,会出现此问题。如果删除一条记录,它将最终删除另一个表中的其他记录,这些记录最终将从另一个表中删除更多记录,这些记录最终将从您开始的表中删除记录,并且循环将重复。真正的灾难性,是开发商最糟糕的噩梦。要解决此问题,请在迁移中添加自定义属性:
newInstance
将Object instance = clazz.newInstance();
executeCommand.invoke(instance, new Object[]{2147483647});
设置为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
至false
受影响的表/(支持的模型类)并相应地更新数据库。
简单地说,你的关系是循环的,例如: 客户 - >薪水 - >工资单 - >客户强> 因此,当您删除客户时,其相应的工资记录将被删除,这会删除所谓的工资核算记录,这些记录将循环回删除链接的客户,并且此循环不断重复导致混乱。因此,实体框架和SQL很好地理解了这一点,并提示用户关闭受影响/错误的删除/更新连接。
答案 1 :(得分:0)
Okey,我将cascadeDelete改为false:
public void ExpenseRowColor()
{
int i;
string PaidStatus;
for (i = 1; i <= ExpensesDataGridView.Rows.Count; i++)
{
PaidStatus = ExpensesDataGridView.Rows[i].Cells[4].Value.ToString();
if (PaidStatus == "Yes")
{
ExpensesDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.LightGreen;
}
}
}
它有效;)