Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core NHibernate System.Data.SQLite could not be found

I am writing a Console App on Net Core 2.0. My App uses NHibernate Fluent to work with SQLite local db.

This is how I configure 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();
    }
}

When I try to build configuration I receive an exception: enter image description here

I have added the following references from Nuget:

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

My local DB is created using SQLite Studio and database type=System.Data.SQLite.

What I am doing wrong? Maybe I am using wrong provider?

like image 784
Anton23 Avatar asked Oct 29 '25 14:10

Anton23


1 Answers

I found a solution.


Step 1 Use package [System.Data.SQLite.Core 1.0.109] in your project.

Step 2 Download package [System.Data.SQLite.Core 1.0.109.1].

Step 3 Copy [runtimes] folder which in package [System.Data.SQLite 1.0.109.1] to your project's folder with follow file mapping.

[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

Then your *.csproj file should include follow content.

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

Step 4 Just build and run your application.

PS:casue package [System.Data.SQLite.Core 1.0.109] is unlisted,you should use package [System.Data.SQLite.Core 1.0.109] by modify *.csproj file with text editor.

like image 101
Zhang Peng Avatar answered Oct 31 '25 04:10

Zhang Peng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!