Cascade delete not deleting orphan children

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
flycast
Posts: 8
Joined: Mon 04 Feb 2019 19:39

Cascade delete not deleting orphan children

Post by flycast » Mon 11 Feb 2019 01:10

When I delete the parent record it leaves the children records hanging with a key value in the foreign key. If I delete all the child records it leaves the parent record hanging. I am expecting that when the last record is deleted on either side then the associated records will delete. Delete cascaded is set to true in the association. Both key fields are Int32. The table SQL looks like this in SQLite Browser:

Code: Select all

CREATE TABLE `FirstParts` (
	`FirstPartID`	INTEGER PRIMARY KEY AUTOINCREMENT,
	`DateTime`	TEXT,
	`ToolID`	INT32 NOT NULL,
	CONSTRAINT `FK_FirstParts_Tools` FOREIGN KEY(`ToolID`) REFERENCES `Tools`(`ToolID`) ON DELETE CASCADE
);

Code: Select all

CREATE TABLE `Tools` (
	`ToolID`	INTEGER PRIMARY KEY AUTOINCREMENT,
	`ToolNumber`	TEXT
);
My code looks like this:

Code: Select all

            using (var context = new Entities())
            {
                var query = (from t in context.Tools
                             select t).FirstOrDefault();

                if (query != null)
                {
                    context.Tools.DeleteObject(query);
                    context.SaveChanges();
                }
            }

flycast
Posts: 8
Joined: Mon 04 Feb 2019 19:39

Re: Cascade delete not deleting orphan children

Post by flycast » Tue 12 Feb 2019 15:26

No thoughts on this? Why would my child records not be deleted?

Shalex
Site Admin
Posts: 8511
Joined: Thu 14 Aug 2008 12:44

Re: Cascade delete not deleting orphan children

Post by Shalex » Wed 13 Feb 2019 19:50

Please, open Association Editor in Entity Developer and make sure that the "Delete Cascaded" option is selected. Save the model to regenerate the code. Does this help? If not, send us a small test project for reproducing the issue.

flycast
Posts: 8
Joined: Mon 04 Feb 2019 19:39

Re: Cascade delete not deleting orphan children

Post by flycast » Wed 13 Feb 2019 21:43

Cascade is already checked. I will try to duplicate in a small test project.

Shalex
Site Admin
Posts: 8511
Joined: Thu 14 Aug 2008 12:44

Re: Cascade delete not deleting orphan children

Post by Shalex » Wed 20 Feb 2019 17:03

Please add "Foreign Key Constraints=On;" to your connection string to fix the issue. Refer to https://www.devart.com/dotconnect/sqlit ... tring.html.

Post Reply