Getting Fluent NHibernate to Work with SQLite

I am sure there is something simple that I did not do, but I am trying to get Fluent NHibernate to work with Sqlite on my machine.

I used NuGet to download nhibernate for free and added the following entity and display:

public class Customer { public virtual string CustomerCode { get; set; } public virtual string Name { get; set; } } public class CustomerMap : ClassMap<Customer> { public CustomerMap () { Id(x => x.CustomerCode); Map(x => x.Name); Table("tblCustomer"); } } 

Then, after starting the free tutorial, I added the following code to the Windows Command project:

 class Program { static void Main(string[] args) { var sessionFactory = CreateSessionFactory(); using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { var customer = new Customer { CustomerCode = "123", Name = "Bob" }; session.SaveOrUpdate(customer); transaction.Commit(); } } } private static ISessionFactory CreateSessionFactory() { return Fluently.Configure() .Database( SQLiteConfiguration.Standard .UsingFile("firstProject.db") ) .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>()) .ExposeConfiguration(BuildSchema) .BuildSessionFactory(); } private static void BuildSchema(Configuration config) { // delete the existing db on each run if (File.Exists("firstProject.db")) File.Delete("firstProject.db"); // this NHibernate tool takes a configuration (with mapping info in) // and exports a database schema from it new SchemaExport(config) .Create(false, true); } } 

Finally, I added the Sqlite dll using NuGet .. however, when I try to start the program, I get the following error:

Top exception:

 An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail. 

The following exception:

 Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4. 

Inner majority of exceptions:

 Unable to find the requested .Net Framework Data Provider. It may not be installed. 

This is when he tries to create a factory session.

Can anyone help with this? Am I running a 32 bit machine?

thanks

Dave

+10
source share
7 answers

You need two things:

  • The System.Data.SQLite link in your project.
  • Include sqlite3.dll , but you can also add a link to sqlite3.dll because it is an unmanaged dll. Just add it as an element to the solution and set to copy it to the output directory.
+12
source

You need a .NET provider for Sqlite. Download it here http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

+5
source

After the initial research, I thought that this was due to the fact that at that time my System.Data.SQLite assembly was not loaded into memory, so I included code to preload the system.Data.SQLite assembly. However, a real error occurred while starting the application:

The mixed-mode assembly is built in comparison with the runtime version "v2.0.50727" and cannot be loaded into runtime 4.0 without additional configuration information.

To fix this, I changed my app.config to look like this:

 <?xml version="1.0"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration> 

with the important bit useLegacyV2RuntimeActivationPolicy = "true"

+5
source

Today I had the same problem, and I spent an hour finding a solution. Anyway, I found it here:

http://thelongwayback.wordpress.com/2009/11/02/fluent-nhibernate-sqlite-problem-the-idbcommand-and-idbconnection-implementation-in-the-assembly-system-data-sqlite-could-not- be-found /

Basically, you will have to use an older version of SQLite (v1.0.60) from here: http://sourceforge.net/projects/sqlite-dotnet2/files/

In another note, I got the same code that worked yesterday on a machine with VS2010 SP1. The same code will not work today on a machine without SP1. If someone gets a chance to test this one, that is, by installing VS2010 SP1, let me know the result, please.

+1
source

None of the above solutions, or any other that I could find on the Internet, worked for me ... until ...

I had to install both x64 and x86 versions of SQLite (via the Vadim link: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki ), in particular the order.

I originally installed the 32-bit version before the 64-bit version, which doesn’t fix anything. On a whim, I deleted them both and reinstalled them in reverse order. Now it works!

I tested this on two different machines and checked that it was a fix for both of them. Awful, but effective.

+1
source

I also ran into this problem.
I noticed that the starter sample project is aimed at .net framework 3.5 , and the one I created (exercise project) is aimed at the client profile .net framework 4.0 (by default vs2010).
I changed the target version to 3.5 and I was able to work on my exercise project.

0
source

In my case, it helped install System.Data.SqLite via NuGet.

So I opened the "Tools / NuGet Package Manager / Package Manager Console" and typed:

Install-Package System.Data.SqLite

0
source

All Articles