none
Perform Cascade delete in mysql code first approach RRS feed

  • Question

  • hi,

    I want to perform cascade delete .I am newbie to entity framework.My database is in mysql .Below are my entities.

    publicclassCounterCategory     {         public CounterCategory()         {             Counters = newList<Counter>();         }         [Key]         publicvirtuallong SId { getset; }//PKpublicvirtuallong PId { getset; }//ProviderIdpublicvirtualstring Name { getset; }         publicvirtualstring Id { getset; }         publicvirtualstring Description { getset; }         publicvirtualint Status { getset; }         publicvirtualstring Properties { getset; }         publicvirtualDateTime SubmitDateTime { getset; }               [ForeignKey("Category")]         publicvirtualList<Counter> Counters { getset; }

        }

                                           
     public class Counter
        {
            [Key]
            public long CId { getset; }    
            public long Category { getset; }
            public string Name { getset; }
            public string Id { getset; }
            public long Value { getset; }
            public int IsAnalytics { getset; }
            public int Status { getset; }
            public string Properties { getset; }
            public DateTime SubmitDateTime { getset; }      
        }
     

    publicclassServiceBladeEntities : DbContext     {               publicDbSet<CounterCategory> CounterCategories { getset; }               publicDbSet<Counter> Counters { getset; }         protectedoverridevoid OnModelCreating(DbModelBuilder modelBuilder)         {        

                   modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();                     }     }

    My Contoller class trying to perform delete but getting error:

      public ActionResult DeleteCategory(long id)
            {
                var category = _db.CounterCategories.ToList().Find(z => z.SId == id);                   
                _db.Entry(category).State = EntityState.Deleted;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

          Thanks,

    Krutik

    Wednesday, April 10, 2013 1:56 PM

Answers

  • Hi Krutik,

    Do you get any exception with the code snippet?

    I think the reason may be caused the counter's state will not change to delete after you set the state of the category instance. The foreign key constraint may be broken. You can try to change the code to to see whether cascade delete works:

    _db.CounterCategories.Remove(category);
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 15, 2013 7:32 AM
    Moderator

All replies

  • Hi Krutik,

    Do you get any exception with the code snippet?

    I think the reason may be caused the counter's state will not change to delete after you set the state of the category instance. The foreign key constraint may be broken. You can try to change the code to to see whether cascade delete works:

    _db.CounterCategories.Remove(category);
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 15, 2013 7:32 AM
    Moderator
  • hi Chester Hong,

    I will check out and let you know about it.

    Thanks for your response.

    Krutik

    Monday, April 15, 2013 7:38 AM
  • hi Chester Hong,

    I tried this but i didn't worked.It deleted entries from categories but not from counters.

    _db.CounterCategories.Remove(category);

    Thanks,

    Krutik

    Monday, April 15, 2013 7:51 AM
  • Hi Krutik,

    I can use the code to perform a cascade delete on my sql express database. I think you may check whether the delete rule is cascade in your database. This is a screenshot from SQL Server:

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 15, 2013 11:58 AM
    Moderator