如何处理由Entity Framework内部创建的SQLiteCommand? [英] How to dispose SQLiteCommands created internally by Entity Framework?

查看:163
本文介绍了如何处理由Entity Framework内部创建的SQLiteCommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在打开新连接时,将System.Data.SQLite(v1.0.94.0)写入SQLite错误(5):数据库被锁定到日志中有问题。这似乎是由Entity Framework(v6.0)引起的,而不是处理所有的SQLiteCommands。

I have problems with System.Data.SQLite (v1.0.94.0) writing "SQLite error (5): database is locked" to the log when opening a new connection. This seems to be caused by Entity Framework (v6.0) not disposing all the SQLiteCommands.

我正在使用代码首先实体框架,因此不能使用这个

I am using code first with Entity Framework and can therefore not use this.

一个场景,其中没有处理SQLiteCommand,正在使用IQueryable。

One scenario, where the SQLiteCommand is not disposed, is using the IQueryable.

IQueryable<TEntity> query = context.Set<TEntity>();
var result = query.ToList();

如何跟踪这些未被隐藏的命令,所以我可以手动处理它们?

How do I keep track of these undisposed commands, so I can dispose them manually?

我想避免调用 GC.Collect();

推荐答案

这个讨论似乎说,定义互操作遗留设置是一种解决方法(而不是回到旧版本的工作... http://www.mail-archive.com/sqlite-users%40sqlite.org/msg75310.html

This discussion seems to say that defining interop legacy settings is a workaround (instead of going back to an older version that works... http://www.mail-archive.com/sqlite-users%40sqlite.org/msg75310.html


在这种情况下,根本原因是实体框架间接创建
SQLiteCommand对象,然后随后
此外,它似乎并没有公开这些内部创建的命令,也没有明确处理它们的方法,没有任何方式让外部的调用者进行清理。这似乎很奇怪,因为几乎所有的IDbCommand实现在野外会可能需要一种或多种本地资源。此外,即使由.NET Framework提供的DbCommand基类也实现了IDisposable(即用作SQLiteCommand的基类的类)。


是的,SQLite中的InteropLegacyClose MSBuild属性。 NET.Settings.targets文件需要设置为true,并定义INTEROP_LEGACY_CLOSE = 1;需要添加到SQLite.Interop.20XX中的INTEROP_EXTRA_DEFINES属性。[vs]正在使用的Visual Studio版本的props文件。

更新: dotConnect for SQLLite查找业务 http://www.devart.com/dotconnect/sqlite/ 有一个免费的专业版,不是免费的可能值得一试。

UPDATE: dotConnect for SQLLite looks the business http://www.devart.com/dotconnect/sqlite/ there is a free and pro version which isn't free... might be worth checking it out.

更新2:为了回应如果这是EF的错误,使用它的每个人都会即使在定位SQL Server时也会出现问题 - Panagiotis Kanavos

UPDATE2: In response to " If this was a bug with EF, everyone that uses it would have issues even when targeting SQL Server. – Panagiotis Kanavos"

根本看来,这个错误只能由SQLLite用户在1.0.8xxx附近报告,在那里他们改变了处理方法... EF提供者可以例如,您可以将其设置为SQLLite(报告错误)或SQLServer(未报告错误)等等,如下所示...

Not at all, it seems the bug is only reported by SQLLite users around 1.0.8xxx where they changed their dispose methods... the EF provider can vary, e.g. you could set it to SQLLite (bug reported) or SQLServer (bug not reported) etc like below...

<system.data>
  <DbProviderFactories>
    <add name="SQLite Data Provider"
          invariant="System.Data.SQLite"
          description="Data Provider for SQLite"
          type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
  </DbProviderFactories>
</system.data>

<providers> 
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
</providers>

这篇关于如何处理由Entity Framework内部创建的SQLiteCommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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