none
Won't Cascade Delete RRS feed

  • Question

  • Complete noob, trying to learn EF.  I have a data model as follows:

        public class Address
        {
            public int AddressID { get; set; }
            public string Street { get; set; }
            public string CityStateZip { get; set; }
        }
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public Address EmployeeAddress { get; set; }
        }
        public class Client
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public Address ClientAddress { get; set; }
        }
        public class Vendor
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public Address VendorAddress { get; set; }
        }

    I'm using "code first" migration in EF Core to generate the database.  Everything works as you would expect except for deletes.  Deleting any of the parent objects (i.e. employee) will not cascade  delete the child (Address).  I've even tried changing the migration script from:

    table.ForeignKey(onDelete: ReferentialAction.Restrict);

    to

    table.ForeignKey(onDelete: ReferentialAction.Cascade);

    The database design screen even shows:

    ([AddressID]) ON DELETE CASCADE

    Any help would be appreciated.

    Thursday, June 20, 2019 5:55 PM

All replies

  • Hi wgcampbell,

    About "Cascade Delete", here I found some document maybe you can refer to.

    Cascade Delete,

    Cascade Delete,

    Cascade deleting with EF Core.

    Hope these can help you.

    Regards,

    Kyle

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 21, 2019 6:14 AM
    Moderator
  • Myself, I configure the DB engine to do cascade deletes, just delete the parent record and let the DB engine delete the records form child tables.

     
    • Proposed as answer by John Boncek Monday, June 24, 2019 5:02 PM
    Friday, June 21, 2019 11:15 AM