Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify ON DELETE NO ACTION in ASP.NET MVC 4 C# Code First

How do I specify ON DELETE NO ACTION Foreign Key Constraint in my model designs?

At present, I have:

public class Status {     [Required]     public int StatusId { get; set; }      [Required]     [DisplayName("Status")]     public string Name { get; set; } }  public class Restuarant {     public int RestaurantId { get; set; }     [Required]     public string Name { get; set; }     [Required]     [EmailAddress]     public string Email { get; set; }     [Required]     public string Telephone { get; set; }     [Required]     public int StatusId { get; set; }     public List<Menu> Menus { get; set; }      // NAVIGATION PROPERTIES     public virtual Status Status { get; set; } }  public class Menu {     public int MenuId { get; set; }      [Required]     public int RestaurantId { get; set; }      [Required]     public string Name { get; set; }      [Required]     public int StatusId { get; set; }      // NAVIGATION PROPERTIES     public virtual Status Status { get; set; }     public virtual Restaurant Restaurant { get; set; } } 

And my DbContext:

public class MenuEntities : DbContext {     public DbSet<Status> Statuses { get; set; }     public DbSet<Restaurant> Restaurants { get; set; }     public DbSet<Menu> Menus { get; set; } } 

As you can see:

  • a Restaurant has many menus
  • a Restaurant has one status
  • a Menu belongs to 1 restaurant
  • Both Restaurants and Menus have 1 status. (Live, Invisible, Draft)

Naturally, if a status is deleted, I certainly don't want to cascade as this will muck everything up.

UPDATE:

Mark Oreta mentions using the following in his example below:

modelBuilder.Entity<FirstEntity>()      .HasMany(f => f.SecondEntities)      .WithOptional()      .WillCascadeOnDelete(false);  

Where do I put this code? Within my MenuEntities / DbContext class? Can anybody provide an example of this being used?

UPDATE: Got this bit working now, however this has created a multiplicity constraint error when trying to seed the DB...

Multiplicity constraint violated. The role 'Menu_Status_Source' of the relationship 'LaCascadaWebApi.Models.Menu_Status' has multiplicity 1 or 0..1. 

My Database Initialiser:

http://pastebin.com/T2XWsAqk

like image 259
Gravy Avatar asked Oct 13 '12 00:10

Gravy


People also ask

What is nonnon-action in MVC?

Non-Action is another built-in attribute which indicates that a public method of a Controller is not an action method. It is used when we don't want that method to be treated as an action method. Please read my previous articles to learn more about Selectors in ASP.NET MVC. ActionName Selectors in ASP.NET MVC

How to add mvc5 controller in MVC 5?

Right-click on Controllers folder add a controller. A window will appear. Choose MVC5 Controller-Empty and click "Add". After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add".

What is nonnon-action in Salesforce controller?

Non-Action is another built-in attribute which indicates that a public method of a Controller is not an action method. It is used when we don't want that method to be treated as an action method.

How do I get the details of a movie using MVC?

Open the Movie controller and examine the Details method. The MVC scaffolding engine that created this action method adds a comment showing a HTTP request that invokes the method. In this case it's a GET request with three URL segments, the Movies controller, the Details method and a ID value.


1 Answers

You can either disable it for your entire context by removing the cascade delete convention in the OnModelCreating method:

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

or, you can do it per relationship using a fluent mapping (also in the OnModelCreating):

EDIT: you would put it in your menu entities

public class MenuEntities : DbContext {     public DbSet<Status> Statuses { get; set; }     public DbSet<Restaurant> Restaurants { get; set; }     public DbSet<Menu> Menus { get; set; }        protected override void OnModelCreating( DbModelBuilder modelBuilder )       {           modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();       modelBuilder.Entity<Menu>()         .HasRequired( f => f.Status )         .WithRequiredDependent()         .WillCascadeOnDelete( false );       modelBuilder.Entity<Restaurant>()         .HasRequired( f => f.Status )         .WithRequiredDependent()         .WillCascadeOnDelete( false );        }  } 
like image 67
Mark Oreta Avatar answered Oct 02 '22 14:10

Mark Oreta