级联删除上不与EF级联 [英] Cascade on delete not cascading with EF

查看:140
本文介绍了级联删除上不与EF级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表一个简单的SQLite数据库。当我手动删除(使用SQLite专家)表中的数据集的条目,预计在OneD的coresponding条目被删除。当我删除实体框架数据集中的一个条目它不会导致一个D的coresponsing条目被删除。没有产生错误。



任何想法,为什么?



问候



下面是数据库的定义:

  CREATE TABLE [数据集(
[资料集] INTEGER NOT NULL ON CONFLICT FAIL PRIMARY KEY AUTOINCREMENT,
[说明] TEXT(128));

CREATE TABLE [OneD(
[OneDId] INTEGER NOT NULL ON CONFLICT FAIL PRIMARY KEY ON冲突中止AUTOINCREMENT,
[资料集] INTEGER NOT NULL ON CONFLICT FAIL独创On冲突ABORT参考文献[数据集]([资料集])ON DELETE CASCADE,
[StockSheetLength] INTEGER NOT NULL ON CONFLICT FAIL);

下面是我如何删除EF入门

 从context.DataSets选择DS DS VAR数据集=; 
的foreach(在数据集变种DS)
context.DataSets.DeleteObject(DS);

context.SaveChanges();
返回真;


解决方案

从SQLite的文档:的 http://www.sqlite.org/foreignkeys.html




外键约束是禁用的默认值(向后
兼容),因此必须对每个数据库
分别连接单独启用。




难道这是你的问题?我不知道,如果你的实体框架与打开它默认为:

  sqlite的> PRAGMA foreign_keys = ON; 



编辑:看远一点我偶然发现了这一点:的 http://nitoprograms.blogspot.com/2010_06_01_archive.html




实体框架实际上是为
本身的包装ADO.NET数据提供(SQLite的,是具体的)一个ADO.NET数据提供程序。
通常情况下,实体框架将打开一个数据库连接
,每当它需要之一;这些自动打开的连接是
,当实体框架是用完后自动关闭。
此默认行为与SQL Server效果很好,因为它ADO.NET
提供商的连接池。但是,它不能很好地与
SQLite的工作,由于各种属性现有的SQLite的连接
本身。其中一个例子是PRAGMA foreign_keys = ON,它执行
外键只对SQLite数据库的连接。如果实体
框架打开和关闭其随意连接,然后SQLite的
编译指示如这些都将丢失。



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;

解决方案

From the SQLite documentation: http://www.sqlite.org/foreignkeys.html

Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled separately for each database connection separately.

Could this be your problem? I don't know if your Entity Framework turns it on by default with:

  sqlite> PRAGMA foreign_keys = ON;

Edit: Looking a bit further i stumbled across this: http://nitoprograms.blogspot.com/2010_06_01_archive.html

The Entity Framework is actually an ADO.NET data provider that is itself wrapping an ADO.NET data provider (SQLite, to be specific). Normally, the Entity Framework will open a database connection whenever it needs one; these automatically-opened connections are automatically closed when the Entity Framework is finished with it. This default behavior works well with SQL Server due to its ADO.NET provider's connection pooling. However, it does not work well with SQLite, due to various "properties" existing on the SQLite connection itself. One example is "PRAGMA foreign_keys = ON", which enforces foreign keys only for that SQLite database connection. If the Entity Framework opens and closes its connections at will, then SQLite PRAGMAs such as these are lost.

这篇关于级联删除上不与EF级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆