【问题标题】:Net Core NHibernate System.Data.SQLite could not be found找不到 Net Core NHibernate System.Data.SQLite
【发布时间】:2018-12-28 22:23:20
【问题描述】:

我正在 Net Core 2.0 上编写控制台应用程序。我的应用程序使用 NHibernate Fluent 来处理 SQLite 本地数据库。

这就是我配置 NHibernate 的方式:

public class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    public static ISession OpenSession()
    {
        var dbConnectionString = @"Data Source=|DataDirectory|\TestDb.sqlite;Version=3;Password=;Foreign Keys=True;Page Size=1024";

        _sessionFactory = Fluently.Configure().Database(SQLiteConfiguration.Standard.ConnectionString(dbConnectionString)
                .ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHibernateHelper>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg)
                .Create(true, true))
            .BuildSessionFactory();

        return _sessionFactory.OpenSession();
    }

    public static void CloseSession()
    {
        _sessionFactory.Close();
        _sessionFactory.Dispose();
    }
}

当我尝试构建配置时,我收到一个异常:

我从 Nuget 添加了以下引用:

  • FluentNHibernate (2.1.2)
  • NHibernate (5.1.3)
  • System.Data.SQLite (1.0.108)

我的本​​地数据库是使用 SQLite Studio 和数据库类型=System.Data.SQLite 创建的。

我做错了什么?也许我使用了错误的提供商?

【问题讨论】:

  • 为什么不是System.Data.SQLite.Core? SQLite 程序集未部署到运行代码期望找到它们的位置。 SQLite 文档中有关于如何管理的内容吗?
  • 我试过了,我收到下一个警告:“警告 NU1701:使用 '.NETFramework,Version=v4.6.1' 而不是项目目标框架 '.NETCoreApp,Version=v2.0'。此包可能与您的项目不完全兼容”@DavidOsborne
  • 啊。我想我已经将“核心”部分与 .Net Core 混淆了。我不认为他们是一样的。
  • 我不确定 SQLite 对 .Net Core 的支持情况。扫描文档没有帮助。但是,我注意到在使用System.Data.SQLite.Core 包时,会部署SQLiteInterop.dll 文件并运行代码。您需要找到一种方法将适当的 SQLite 二进制文件放入 NH 代码可以从中解析类型的位置。这是如何使用与 .Net Core 兼容的 SQLite 版本完成的,我现在不知道。

标签: c# sqlite nhibernate .net-core


【解决方案1】:

我找到了解决办法。


步骤 1 在您的项目中使用包 [System.Data.SQLite.Core 1.0.109]。

第二步下载包[System.Data.SQLite.Core 1.0.109.1]。

第 3 步 将包 [System.Data.SQLite 1.0.109.1] 中的 [runtimes] 文件夹复制到您的项目文件夹中,并遵循文件映射。

[package]  -->  [project folder]
runtimes/win-x86/lib/netstandard2.0/SQLite.Interop.dll  -->  runtimes/win-x86/native/SQLite.Interop.dll
runtimes/win-x64/lib/netstandard2.0/SQLite.Interop.dll  -->  runtimes/win-x64/native/SQLite.Interop.dll
runtimes/linux-x64/lib/netstandard2.0/SQLite.Interop.dll  -->  runtimes/linux-x64/native/SQLite.Interop.dll
runtimes/osx-x64/lib/netstandard2.0/SQLite.Interop.dll  -->  runtimes/osx-x64/native/SQLite.Interop.dll

那么你的 *.csproj 文件应该包含以下内容。

<ItemGroup>
    <None Update="runtimes\linux-x64\native\SQLite.Interop.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="runtimes\osx-x64\native\SQLite.Interop.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="runtimes\win-x64\native\SQLite.Interop.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="runtimes\win-x86\native\nssm.exe">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="runtimes\win-x86\native\SQLite.Interop.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
</ItemGroup>

第 4 步构建并运行您的应用程序。

PS:由于包[System.Data.SQLite.Core 1.0.109]未列出,你应该通过修改*.csproj来使用包[System.Data.SQLite.Core 1.0.109]使用文本编辑器的文件。

【讨论】:

  • 是否需要在*.csproj中设置“将所有依赖项复制到输出目录”?
  • 非常感谢!你怎么能做这些映射?在 *.csproj 中?
  • 我在 net core 应用程序中尝试过,但无法构建我的应用程序: 命名空间找不到 System.Data.SQLite。您是否尝试在 NET CORE 控制台应用程序中执行此操作?
  • 这是我的错。是 [System.Data.SQLite.Core] 而不是 [System.Data.SQLite]。我再次编辑了我的答案。
  • 这是我的荣幸。
【解决方案2】:

2020 年 7 月更新 只需将 nuget 包 System.Data.SQLite.Core 添加到您的项目中,错误就会消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多