问题描述
我正在创建一个ASP.Net MVC 3应用程序,并且在尝试使用迁移来更新数据库时遇到了外键约束问题.我使用的是Code-First,并且出现的错误是:
I am creating an ASP.Net MVC 3 application and I am running into a foreign key constraint problem when trying to update my database using migrations. I am using Code-First, and the error I am getting is:
在表"CategoryItemValues"上引入FOREIGN KEY约束"FK_CategoryItemValues_CategoryProperties_CategoryPropertyId"可能会导致循环或多个级联路径.指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束.无法创建约束.请参阅先前的错误.
Introducing FOREIGN KEY constraint 'FK_CategoryItemValues_CategoryProperties_CategoryPropertyId' on table 'CategoryItemValues' 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.
这是我的课程:
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的Delete上禁用Cascade,但是我不确定该怎么做.我知道我需要做类似的事情:
It seems like I need to disable Cascade on Delete for CategoryItemValues, but I am not sure how to do that. I know I need to do something like:
modelBuilder.Entity< ...>().hasRequired(...).和很多(...).HasForeignKey(...).WillCascadeOnDelete(false);
modelBuilder.Entity<...>() .HasRequired(...) .WithMany(...) .HasForeignKey(...) .WillCascadeOnDelete(false);
但是我不能完全正确.
推荐答案
这应该有效...
public class Category
{
public int Id { get; set; }
public string CategoryName { get; set; }
public string DisplayName { get; set; }
public int DisplayOrder { get; set; }
public bool IsTab { get; set; }
public bool Active { get; set; }
public virtual List<CategoryProperty> Properties { get; set; }
public virtual List<CategoryItem> Items { get; set; }
}
public class CategoryProperty
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string PropertyName { get; set; }
public int DisplayOrder { get; set; }
public virtual Category Category { get; set; }
public virtual List<CategoryItemValue> Values { 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; }
}
...和要点" ...
...and the 'gist'...
modelBuilder.Entity<CategoryProperty>()
.HasKey(i => i.Id);
modelBuilder.Entity<CategoryProperty>()
.HasRequired(i => i.Category)
.WithMany(u => u.Properties)
.HasForeignKey(i => i.CategoryId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<CategoryItem>()
.HasKey(i => i.Id);
modelBuilder.Entity<CategoryItem>()
.HasRequired(i => i.Category)
.WithMany(u => u.Items)
.HasForeignKey(i => i.CategoryId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<CategoryItemValue>()
.HasKey(i => i.Id);
modelBuilder.Entity<CategoryItemValue>()
.HasRequired(i => i.Item)
.WithMany(u => u.Values)
.HasForeignKey(i => i.CategoryItemId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<CategoryItemValue>()
.HasRequired(i => i.Property)
.WithMany(u => u.Values)
.HasForeignKey(i => i.CategoryPropertyId)
.WillCascadeOnDelete(false);
这篇关于ASP.Net MVC 3 EF“在表上引入FOREIGN KEY约束可能会导致循环或多个级联路径".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!