我正在创建一个ASP.Net MVC 3应用程序,并在尝试使用迁移更新我的数据库时遇到外键约束问题 . 我正在使用Code-First,我得到的错误是:
在表'CategoryItemValues'上引入FOREIGN KEY约束'FK_CategoryItemValues_CategoryProperties_CategoryPropertyId'可能会导致循环或多个级联路径 . 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束 . 无法创建约束 . 查看以前的错误 .
这是我的课程:
public class Category
{
public int Id { get; set; }
[Display(Name = "Category Name")]
public string CategoryName { get; set; }
[Display(Name = "Display Name")]
public string DisplayName { get; set; }
[Display(Name = "Display Order")]
public int DisplayOrder { get; set; }
public bool IsTab { get; set; }
public bool Active { get; set; }
public virtual List<CategoryProperty> Properties { get; set; }
}
public class CategoryProperty
{
public int Id { get; set; }
public int CategoryId { get; set; }
[Display(Name="Property Name")]
public string PropertyName { get; set; }
[Display(Name = "Display Order")]
public int DisplayOrder { get; set; }
public virtual Category Category { get; set; }
}
public class CategoryItem
{
public int Id { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual List<CategoryItemValue> Values { get; set; }
}
public class CategoryItemValue
{
public int Id { get; set; }
public int CategoryItemId { get; set; }
public int CategoryPropertyId { get; set; }
public string Value { get; set; }
public virtual CategoryItem Item { get; set; }
public virtual CategoryProperty Property { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// I know that the solution needs to go here!
}
看起来我需要为CategoryItemValues禁用Cascade on Delete,但我不知道该怎么做 . 我知道我需要做以下事情:
modelBuilder.Entity <...>() . HasRequired(...) . WithMany(...) . HasForeignKey(...).WillCascadeOnDelete(false);
但我不能完全正确 .
1 回答
这应该工作......
......和'要点'......