Setting table level WillCascadeOnDelete not available

不打扰是莪最后的温柔 提交于 2019-12-10 15:41:45

问题


I'm pulling out my hair here. I've seen the solutions to turning off cascade on delete here, but I can't implement it. I don't know what I'm doing wrong here, but I keep getting the below error:

'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration' does not contain a definition for 'WillCascadeOnDelete' and no extension method 'WillCascadeOnDelete' accepting a first argument of type 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration' could be found (are you missing a using directive or an assembly reference?)

I've added the necessary namespaces, but I don't see it as an option anywhere in the intellisense and I'm not getting anywhere searching. I'm in VS 2010 MVC 3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vf2.Models;
using vf2.Models.LinkTables;
using vf2.Models.Requests;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration;
using vf2.Models.Reporting;
using vf2.Models.POSObj;

namespace vf2.Models
{
    public class vfContext : DbContext
    {
        public DbSet<App> Apps { get; set; }
        public DbSet<Origin> Origins { get; set; }
        public DbSet<WineType> WineTypes { get; set; }
        public DbSet<VarType> VarTypes { get; set; }
        public DbSet<Wine> Wines { get; set; }
        public DbSet<Vintage> Vintages { get; set; }

        public DbSet<Distributor> Distributors { get; set; }
        public DbSet<Importer> Importers { get; set; }
        public DbSet<Producer> Producers { get; set; }
        public DbSet<Publication> Publications { get; set; }
        public DbSet<Review> Reviews { get; set; }
        public DbSet<UserType> UserTypes { get; set; }
        public DbSet<Restaurant> Restaurants { get; set; }

        public DbSet<WineListChangeRate> WineListChangeRates { get; set; }
        public DbSet<MenuChangeRate> MenuChangeRates { get; set; }
        public DbSet<WineListCount> WineListCounts { get; set; }

        public DbSet<UserObj> UserObjs { get; set; }
        public DbSet<ProducerUser> ProducerUsers { get; set; }
        public DbSet<DistributorUser> DistributorUsers { get; set; }
        public DbSet<RestaurantUser> RestaurantUsers { get; set; }

        public DbSet<ProducerEditRequest> ProducerEditRequests { get; set; }
        public DbSet<RequestStatus> RequestStatuses { get; set; }
        public DbSet<VOAVIRequest> VOAVIRequests { get; set; }

        public DbSet<POS> POSs { get; set; }
        public DbSet<Cart> Carts { get; set; }
        public DbSet<FutureUser> FutureUsers { get; set; }
        public DbSet<Doc> Docs { get; set; }
        public DbSet<DocType> DocTypes { get; set; }

        public DbSet<WineVisit> WineVisits { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Review>().WillCascadeOnDelete(false);
//error here!

            base.OnModelCreating(modelBuilder);
        }
    }
}

回答1:


"Cascading delete" is a configuration of a relationship, not of an entity/table. Hence WillCascadeOnDelete is a method of CascadableNavigationPropertyConfiguration. Use case example:

modelBuilder.Entity<Review>()
    .HasRequired(r => r.Wine)
    .WithMany()
    .WillCascadeOnDelete(false);

It means that if a wine is deleted from the catalog in the database, it's reviews should not be deleted together with the wine. That's a property of this specific relationship, not of the Reviews table.

In this case trying to delete a wine which has reviews would result in a foreign key constraint violation and exception of course, but that is what you usually want when you disable cascading delete on a required relationship ("Don't allow to delete a wine which has reviews, only allow it for wines which haven't any...").



来源:https://stackoverflow.com/questions/10884940/setting-table-level-willcascadeondelete-not-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!