System.Data.SQLite和SubSonic 3:在VS2008中效果很好,但在VS2010中效果不佳



去年,我在VS2008项目中非常成功地使用了SubSonic 3和SQLite,并对结果感到非常满意。就在最近,我试图在VS2010项目中设置SubSonic 3和SQLite,但在尝试实例化一个新的SimpleRepository:时遇到了内部异常

的类型初始值设定项'System.Data.SQLite.SQLiteFactory'引发异常

为了确保我没有发疯,我在VS2008中尝试了完全相同的代码和app.config文件,没有问题。奇怪的

目前,我使用的是SubSonic 3.0.0.4和x64版本的System.Data.SQLite 1.0.73.0(3.7.6.3)(尽管我也尝试了32位版本)。我添加了这两个DLL作为引用,并将"复制本地"设置为TRUE。

我的应用程序配置看起来像:

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="myDatabase" connectionString="Data Source=C:DBmydatabase.db3" providerName="System.Data.SQLite"/>
  </connectionStrings>

我的代码看起来像:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SubSonic.Repository;
namespace SubSonicSqliteTestConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var repo = new SimpleRepository("myDatabase", SimpleRepositoryOptions.RunMigrations);
        }
    }
}

有什么想法吗?

我将SubSonic 3与Visual Studio 2010和SQLite 1.0.66一起使用,它可以工作。然而,有几件事你必须做:

  • 您的应用程序必须是x86(而不是AnyCPU),否则在64位计算机上会出现BadImageFormatException
  • 您必须将目标框架从"Framework 4.0客户端配置文件"更改为"Framework 4.0"
  • 您必须将其添加(或修改)到app.config文件中。如果没有useLegacyV2RuntimeActivationPolicy标志设置为true ,SQLite将无法工作

    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    
  • 我的工厂初始化代码是这样的(它包括Version和PublicKeyToken)。我从我运行安装程序的开发机器上的machine.config文件中获得了这些设置,并选择将SQLite集成到Visual Studio 2010(从All Programs\SQLite文件夹中)。文件位于%WinDir%Microsoft.NETFramework<FrameworkVersion>CONFIG

    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SQLite" />
            <add name="SQLite Data Provider" invariant="System.Data.SQLite"
                       description=".Net Framework Data Provider for SQLite"
                       type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
        </DbProviderFactories>
    </system.data>
    
  • 我的连接字符串在路径中包含斜杠而不是反斜杠,并且包含版本

    <connectionStrings>
        <add name="connectionstringname"
                   connectionString="Data Source=c:/temp/mydatabase.db;Version=3;"
                   providerName="System.Data.SQLite"/>
    </connectionStrings>
    

希望能有所帮助。

更新:我看到你使用的是x64版本的SQLite,所以忘记第一个提示。但我离开它,也许它对其他人有帮助。

如果我没记错的话,"的类型初始值设定项…引发了异常"错误消息通常意味着该类型的静态构造函数中有东西引发了异常。您可能想尝试在Reflector、dotPeek、JustDecompile或任何您喜欢的工具中打开System.Data.SQLite.SQLiteFactory,看看它的静态构造函数(.ctor)在做什么。你在VS2010中使用.NET 4,在VS2008中使用.NET 2/3吗?这可能也是问题的一部分。

相关内容

最新更新