Search code examples
mysqlasp.net-mvccascadecascading-deletes

Cascade Delete for MySQL and ASP.NET MVC4


I have create simple asp.net (MVC4) web page with mysql database. There I have two tables (persons and orders), where table orders have FORIGN Key Persons_ID. I whant to create delete function, so, when I delete person from persons table, it will also delete all orders from order table for this person.

For creating models I have used ADO.NET and it create this two models for each tables:

persons.cs using System.ComponentModel.DataAnnotations;

namespace MvcMySQLTest1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class person
    {
        public person()
        {
            this.orders = new HashSet<order>();
        }

        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Adress { get; set; }
        public string City { get; set; }

        public virtual ICollection<order> orders { get; set; }
    }
}

orders.cs

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace MvcMySQLTest1.Models
{
    using System;
    using System.Collections.Generic;

    public partial class order
    {
        public int O_Id { get; set; }
        public int OrderNo { get; set; }
        public Nullable<int> Persons_Id { get; set; }

        public virtual person person { get; set; }

    }
}

I have also create MainModel - like I container for both models above:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace MvcMySQLTest1.Models
{
    public class MainModel 
    {

        public person Persons { get; set; }

        public order Orders { get; set; }

    }
}

Now for Cascade deleting I have try this - so when I delete Person, it will also delete all Orders for this Person in order table, but this seems not to work:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace MvcMySQLTest1.Models
{
    public class MainModel : DbContext //added
    {

        public person Persons { get; set; }

        public order Orders { get; set; }

        //added  
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

        modelBuilder.Entity<orders>()
          .HasOptional(a => a.persons)
          .WithOptionalDependent()
          .WillCascadeOnDelete(true);

        base.OnModelCreating(modelBuilder);
        }
    }
}

Solution

  • May be you can try something like this

    modelBuilder.Entity<order>() 
        .HasRequired(a => a.person) 
        .WithMany(t => t.order) 
        .HasForeignKey(d => d.Persons_Id) 
        .WillCascadeOnDelete(true);
    

    Or try to read more here MSDN Cascade Delete. May something can help from there