Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting table level WillCascadeOnDelete not available

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);
        }
    }
}
like image 513
user576838 Avatar asked Jan 17 '23 12:01

user576838


1 Answers

"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...").

like image 145
Slauma Avatar answered Feb 15 '23 09:02

Slauma