Cascade delete is not working correctly in EF6 Code-first
Posted: Thu 25 Oct 2018 05:58
Hello.
I'm using EF6 Code-first and I have Entites with cascade deleting enabled. Database is genereated correctly (cascade delete is enabled fo relations). Database schema is the following:
When I delete FlightExpress I expect cascade delete for all related ExpressEvents, EventParameters and ParameterValues. But in fact that is working very strange:
- if I put a breakpoint after my delete method (line 27 in BlankDB.DAL.Test\Program.cs) - only FlightExpress is deleted from database. That is working that way even if I begin a transaction in DbAccess.DbStartBlock method and commit it in DbAccess.DbFinallyBlock method.
- if I put a breakpoint after DbAccess.Context.Expresses.Remove(express) but before DbAccess.DbFinallyBlock where I close connection to database and perform SaveChanges() (line 85 in BlankDB.DAL\BlankDB.Interop.cs) and stand on it for some time - FlightExpress with related ExpressEvents, EventParameters and ParameterValues are deleted.
Looks like database (or your driver) needs some wait time before connection closing. But howto calculate it?
I use dotConnect for SQLite 5.11.1202 and EF6.
I've uploaded sample project here. Set BlankDB.DAL.Test as startup project, compile and run.
I'm using EF6 Code-first and I have Entites with cascade deleting enabled. Database is genereated correctly (cascade delete is enabled fo relations). Database schema is the following:
When I delete FlightExpress I expect cascade delete for all related ExpressEvents, EventParameters and ParameterValues. But in fact that is working very strange:
- if I put a breakpoint after my delete method (line 27 in BlankDB.DAL.Test\Program.cs) - only FlightExpress is deleted from database. That is working that way even if I begin a transaction in DbAccess.DbStartBlock method and commit it in DbAccess.DbFinallyBlock method.
- if I put a breakpoint after DbAccess.Context.Expresses.Remove(express) but before DbAccess.DbFinallyBlock where I close connection to database and perform SaveChanges() (line 85 in BlankDB.DAL\BlankDB.Interop.cs) and stand on it for some time - FlightExpress with related ExpressEvents, EventParameters and ParameterValues are deleted.
Looks like database (or your driver) needs some wait time before connection closing. But howto calculate it?
I use dotConnect for SQLite 5.11.1202 and EF6.
I've uploaded sample project here. Set BlankDB.DAL.Test as startup project, compile and run.