ASP.Net MVC 3 EF"上表引进国外KEY约束可能会导致循环或多重级联路径"可能会、路径、国外、级联

2023-09-03 04:04:05 作者:诉说者

我创建一个ASP.Net MVC 3应用程序,我遇到了一个外键约束问题,尝试使用迁移到更新我的数据库时。我使用code-第一,而我得到的错误是:

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:

引进国外KEY约束'FK_CategoryItemValues​​_CategoryProperties_CategoryPropertyId对表CategoryItemValues​​可能会导致循环或多重级联路径。指定ON DELETE NO行动或UPDATE NO ACTION或修改另一个外键约束。 无法创建约束。见previous错误。

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​​,但我不知道该怎么做。我知道我需要做的是这样的:

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&LT; ...>()             .HasRequired(...)             .WithMany(...)             .HasForeignKey(...)             .WillCascadeOnDelete(假);

modelBuilder.Entity<...>() .HasRequired(...) .WithMany(...) .HasForeignKey(...) .WillCascadeOnDelete(false);

但我不能完全理解这一点。

But I cannot get it exactly right.

推荐答案

这应该工作...

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