开发者

[A]System.Data.SQLite.SQLiteConnection cannot be cast to [B]System.Data.SQLite.SQLiteConnection

I've got a bit of a heisenbug on my hands now, and I'm looking to find some help with it.

I have a Windows service that uses System.data.sqlite. In rare cases (apparently only on Windows Vista, but this may be a red herring), the exception occurs: [A]System.Data.SQLite.SQLiteConnection cannot be cast to [B]System.Data.SQLite.SQLiteConnection...

Unfortunately, we haven't been able to repeat this bug here in the office, and have limited access to the machines where it occurs. I've tried messing with the GAC (registering and de-registering assemblies of different versions and providence using g开发者_StackOverflow社区acutil), and the app seems to do fine on my development machine.

Any idea how I might try to reproduce this or what kinds of situations can cause this? Have you run into bugs like this that only occur on Windows Vista?

Here is the fullest stack trace I have:

[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)

Edit: In my app.config, I have provider=System.Data.SQLite; in a couple of places. Notice that there's no strong name or version number. I suspect what may be happening is that in some instances, the DLL containing the data models gets loaded, and when Entity Framework sees that that provider is requested, it chooses a System.Data.Sqlite to use. Then, later when some code happens, a different DLL is loaded according to the referencing rules. This DLL, however, has a different version or strong name than the one loaded by EF, and hence the exception.


I found two other posts from users who've experienced the same issue here:
http://sqlite.phxsoftware.com/forums/t/2554.aspx
and here
http://forums.sabnzbd.org/viewtopic.php?f=6&t=3097&start=150

Are you using the GAC to deploy the SQLite assemblies?
Are you also deploying System.Data.Sqlite.Linq.dll?
You can try to deploy the SQLite dependencies to your bin folder and use strong name assembly references in your configuration file to avoid this issue for example.

I'm thinking maybe this can be reproduced by having conflicting versions of System.Data.Sqlite.dll and\or System.Data.Sqlite.Linq.dll on the same system, but the built-in .net framework assembly resolver will attempt to load one of the in-compatible versions anyway because the dependencies are not strongly referenced by version and\or public key token information.

For debugging \ logging purposes, you can hook into the AssemblyLoad and\or AssemblyResolve AppDomain events:
http://msdn.microsoft.com/en-us/library/1dw188c1.aspx

As a work-around to resolve the issue without re-compiling, copy the missing dependencies to the bin folder and update the configuration file accordingly.

Edit
Also see these posts for more information and a tested solution:
http://sqlite.phxsoftware.com/forums/p/2021/8867.aspx
http://sqlite.phxsoftware.com/forums/p/2333/9615.aspx


I also received this error when trying to create an ADO.NET Entity Data Model.

My solution was loading the design type components for Visual Studio 2013.
My download was 'sqlite-netFx451-setup-bundle-x86-2013-1.0.99.0.exe' found on:

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

My Nuget Packages for SQLite were not matching my designer components.


Try to manually delete the files in ProjectFolder/Packages, remove the references to SQLite from your project and download the new package versions from NuGet or whereever you download them from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜