[A] System.Data.SQLite.SQLiteConnection无法强制转换为[B] System.Data.SQLite.SQLiteConnection

时间:2011-06-13 19:10:06

标签: entity-framework-4 system.data.sqlite dll strongname

我现在手上有一点heisenbug,我正在寻找一些帮助。

我有一个使用System.data.sqlite的Windows服务。在极少数情况下(显然仅在Windows Vista上,但这可能是红色鲱鱼),会发生异常:[A]System.Data.SQLite.SQLiteConnection cannot be cast to [B]System.Data.SQLite.SQLiteConnection...

不幸的是,我们无法在办公室重复此错误,并且对发生错误的计算机的访问权限有限。我已经尝试搞乱GAC(使用gacutil注册和取消注册不同版本和上帝的程序集),该应用程序似乎在我的开发机器上运行良好。

任何想法我怎么可能尝试重现这个或什么样的情况会导致这个?您是否遇到过只出现在Windows Vista上的错误?

这是我拥有的最完整的堆栈跟踪:

[A]System.Data.SQLite.SQLiteConnection cannot be cast to [B]System.Data.SQLite.SQLiteConnection.  
Type A originates from 'System.Data.SQLite, Version=1.0.66.0.  Culture=neutral, PublicKey Token=db937bc2d44ff139' 
in the context 'Default' at location 'C:\Program Files\ultracorp\System.... [yes, ellipses are the best I have right here, I know...]


Server stack trace: 
   at System.Data.SQLite.SQLiteCommand.set_DbConnection(DbConnection value)
   at System.Data.Common.DbCommand.set_Connection(DbConnection value)
   at System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand)
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
   at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
   at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable`1 sequence)
   at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at UltraCorp.DataWrangler.Models.WidgetsTable.getWidget(WidgetsEntities dbConn)
   at UltraCorp.DataWrangler.Models.WidgetsTable.getWidget()
   at UltraCorp.DataWrangler.Models.WidgetsTable.getUser()
   at UltraCorp.DataWrangler.Infrastructure.DataWranglerTray.getUser()
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)

修改:在我的app.config中,我在几个地方都有provider=System.Data.SQLite;。请注意,没有强名称或版本号。我怀疑可能发生的是在某些情况下,包含数据模型的DLL被加载,当实体框架看到请求该提供者时,它选择使用System.Data.Sqlite。然后,稍后当某些代码发生时,根据引用规则加载不同的 DLL。但是,此DLL具有与EF加载的版本不同的版本或强名称,因此也是例外。

3 个答案:

答案 0 :(得分:2)

尝试创建ADO.NET实体数据模型时,我也收到了此错误。

我的解决方案是加载Visual Studio 2013的设计类型组件。
我的下载是'sqlite-netFx451-setup-bundle-x86-2013-1.0.99.0.exe'

https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

我的SQLite Nuget包与我的设计师组件不匹配。

答案 1 :(得分:1)

我在这里遇到了同样问题的用户发现了另外两篇帖子:
http://sqlite.phxsoftware.com/forums/t/2554.aspx
在这里 http://forums.sabnzbd.org/viewtopic.php?f=6&t=3097&start=150

您是否正在使用GAC部署SQLite程序集? 您是否也在部署System.Data.Sqlite.Linq.dll?
您可以尝试将SQLite依赖项部署到bin文件夹,并在配置文件中使用强名称程序集引用来避免此问题。

我想也许这可以通过在同一系统上具有冲突版本的System.Data.Sqlite.dll和\或System.Data.Sqlite.Linq.dll来重现,但是内置的.net框架程序集解析器将尝试加载其中一个兼容的版本,因为版本和\或公钥标记信息没有强烈引用依赖项。

对于调试\日志记录,您可以挂钩到AssemblyLoad和\或AssemblyResolve AppDomain事件:
http://msdn.microsoft.com/en-us/library/1dw188c1.aspx

作为解决问题而无需重新编译的解决方法,将缺少的依赖项复制到bin文件夹并相应地更新配置文件。

修改
另请参阅这些帖子以获取更多信息和经过测试的解决方案:
http://sqlite.phxsoftware.com/forums/p/2021/8867.aspx
http://sqlite.phxsoftware.com/forums/p/2333/9615.aspx

答案 2 :(得分:0)

手动删除 ProjectFolder/Packages 中的文件,从您的项目中删除对 SQLite 的引用,并从 NuGet 或从您下载它们的任何位置下载新的包版本。