I have a simple sqlite database with two tables. When I manually delete (using SQLite Expert)an entry in table DataSets, the coresponding entry in OneD is deleted as expected. When I delete an entry in DataSets from Entity Framework it does not cause the coresponsing entry in One D to be deleted. There is no error generated.
Any idea why?
Regards
Here is the database definition:
CREATE TABLE [DataSets] (
[DataSetId] INTEGER NOT NULL ON CONFLICT FAIL PRIMARY KEY AUTOINCREMENT,
[Description] TEXT(128));
CREATE TABLE [OneD] (
[OneDId] INTEGER NOT NULL ON CONFLICT FAIL PRIMARY KEY ON CONFLICT ABORT AUTOINCREMENT,
[DataSetId] INTEGER NOT NULL ON CONFLICT FAIL UNIQUE ON CONFLICT ABORT REFERENCES [DataSets]([DataSetId]) ON DELETE CASCADE,
[StockSheetLength] INTEGER NOT NULL ON CONFLICT FAIL);
Here is how I delete the entry from EF
var dataSets = from ds in context.DataSets select ds;
foreach (var ds in dataSets)
context.DataSets.DeleteObject(ds);
context.SaveChanges();
return true;
The problem can be solved by enabling foreign keys in the connection string:
data source=mydb.db;foreign keys=true
here's my solution to that problem:
db.Connection.StateChange += ConnectionStateChange;
void ConnectionStateChange(object sender, System.Data.StateChangeEventArgs e)
{
if (e.CurrentState == System.Data.ConnectionState.Open)
db.ExecuteStoreCommand("PRAGMA foreign_keys = true;");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With