【问题标题】:System.Data.SQLite and SubSonic 3: Works great in VS2008, but not in VS2010System.Data.SQLite 和 SubSonic 3:在 VS2008 中效果很好,但在 VS2010 中效果不佳
【发布时间】:2011-09-16 21:08:29
【问题描述】:

去年,我在一个 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。

我的 App.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" />
    </DbProviderFactories>
  </system.data>

  <connectionStrings>
    <add name="myDatabase" connectionString="Data Source=C:\DB\mydatabase.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);
        }
    }
}

有什么想法吗?

【问题讨论】:

    标签: sqlite subsonic subsonic3 system.data.sqlite


    【解决方案1】:

    我正在使用带有 Visual Studio 2010 和 SQLite 1.0.66 的 SubSonic 3,它可以工作。 但是,您必须做一些事情:

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

      <startup useLegacyV2RuntimeActivationPolicy="true">
          <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
      
    • 我的工厂初始化代码如下所示(它包括版本和 PublicKeyToken)。我从运行设置并选择将 SQLite 集成到 Visual Studio 2010(来自 All Programs\SQLite 文件夹)的开发机器上的 machine.config 文件中获得了此设置。该文件位于@%WinDir%\Microsoft.NET\Framework\&lt;FrameworkVersion&gt;\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,所以忘记第一个提示。但是我留下了,也许它对其他人有帮助。

    【讨论】:

    • 有趣的是,Rally25rs 告诉我,我上面的代码在 VS2010(Win7 32 位)中对他来说工作得很好——我最终自己回到了 1.0.66 版本,一切都到位了。现在没有问题。不过,感谢您的建议。
    【解决方案2】:

    如果我没记错的话,“...的类型初始化程序引发了异常”错误消息通常意味着该类型的静态构造函数中的某些内容引发了异常。您可能想尝试在 Reflector 或 dotPeek 或 JustDecompile 或您喜欢的任何工具中打开 System.Data.SQLite.SQLiteFactory,并查看其静态构造函数 (.cctor) 正在做什么。你有 ti 在 VS2010 中使用 .NET 4 和在 VS2008 中使用 .NET 2/3 吗?这也可能是问题的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多