System.Data.Entity.ModelConfiguration.Configuration.ManyToManyNavigationPropertyConfiguration不包含'WillCascadeOnDelete'的定义

时间:2015-06-26 19:10:07

标签: asp.net-mvc entity-framework ef-fluent-api

错误:Error 1 'System.Data.Entity.ModelConfiguration.Configuration.ManyToManyNavigationPropertyConfiguration<>' does not contain a definition for 'WillCascadeOnDelete' and no extension method 'WillCascadeOnDelete' accepting a first argument of type 'System.Data.Entity.ModelConfiguration.Configuration.ManyToManyNavigationPropertyConfiguration<>' could be found (are you missing a using directive or an assembly reference?)

我在OnModelCreating函数的上下文类中遇到此错误:.WillCascadeOnDelete(false)。我无法弄清楚问题是什么。我在网上找到的每个例子都与我的代码完全一样。这是我的OnModelCreating函数:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        //modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

        modelBuilder.Entity<CategoryModel>().ToTable("Categories");
        modelBuilder.Entity<ProductModel>().ToTable("Products");
        modelBuilder.Entity<BrochureModel>().ToTable("Brochures");

        modelBuilder.Entity<ProductModel>()
        .HasMany<CategoryModel>(t => t.CategoryList)
        .WithMany(t => t.ProductList)
        .WillCascadeOnDelete(false)
        .Map(m =>
        {
            m.ToTable("ProductCategory");
            m.MapLeftKey("ProductID");
            m.MapRightKey("CategoryID");
        });
    }

如果需要其他代码,请告诉我。

1 个答案:

答案 0 :(得分:0)

我想出了如何从另一个SO答案中做到这一点。我需要在初始创建类中将.ForeignKey("dbo.Products", t => t.ProductID, cascadeDelete: true)更改为.ForeignKey("dbo.Products", t => t.ProductID, cascadeDelete: false)以进行迁移。这是SO答案:https://stackoverflow.com/a/27716225/571723