#SQLite Database #NuGet Package: Common Problems Solved



SQLite is a file based database, or an embedded database that you can use without need for any special installation from your side. It’s a bit popular for non-LOB (line of business) desktop applications and even mobile applications like Android apps. It also has an "In-Memory" mode where there is no physical file used as a database, which is usually used in testing.

It has a similar syntax (sort of) to SQL Server. In Microsoft .NET world,, those using NHibernate are usually using the in-memory mode for applying unit testing or trying out NHibernate stuff, although Microsoft has its own embedded database called SQL Compact Edition (CE), which doesn’t seem to support in-memory databases (v4.0).


You can download the latest Sqlite database (v3.0) from:


Since it’s not SQL Server, you need a special ADO.NET provider too. It’s called "System.Data.Sqlite"., which is very similar to the normal System.Data.SqlClient stuff. It’s included in the above download link.



If you don’t know what NuGet is, go to www.nuget.org or check my old posts mentioning it, or if you are an Arab maybe check this post in Arabic.

NuGet has become the No. 1 way I use to find libraries, and I usually really get mad if I don’t find the library in there or I have to get a more recent version from library source code, which luckily doesn’t happen that often.

Today I discovered that NuGet already has a NuGet package:



I was willing to make some test to what I believed was a bug in NHibernate 3.2, but this is another story.



I thought (like most other NuGet packages) the package will do configuration needed to up and running with SQLite, but I found that I was still getting different configuration issues.

Here is the basic code I was trying to get to work:



All the code in NHibernate code (even the dialect thing). Don’t bother yourself with it if you don’t know NHibenrate. I just wanted to show the exact path I went into when facing this.

You do NOT need NHibernate to use SQLite, you can use it just like SQL Server under System.Data.Sqlite same as you use System.Data.SqlClient for SQL Server. I’m just covering usual use of it and part of my actual real situation.

The only relevant SQLite part is the connection string, which, as this is for testing, was using in-memory database.


Configuration: Problems & Solutions


I have updated the NuGet package to include the required config entries.

This configuration section is now obsolete and for reference only.


It should be helpful though if you download the SQLite library manually not from NuGet or modified your configuration after installing the package.


The updated packages start from version




When I tried to run this, I got the following error:

Unhandled Exception: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.SQLite20Driver. --->

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->

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

The first 2 lines are NHibernate thing as I know (don’t bother the "2" part in there, just because I tell you not to). The following 2 lines are just generic stuff.

The last two are what really matter, specifically:

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

SQLite has a similar ADO.NET provider like SqlClient for MS SQL Server, we know we have it, but .NET is unable to find it.

To Solve this problem, I created an app.config file (if it’s a website, you’ll use web.config), and I added the following directly under <Configuration> root node:



And then tried to run again…

This above problem "might" happen with the download file included in the website directly also, not only the NuGet package.


Mixed Mode Assembly

Next time the Console had the following errors

Unhandled Exception: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.SQLite20Driver. --->

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->

System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

As usual the important bit is the last bit:

System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

To understand what’s happening, let’s read the SQLite package description in NuGet:

System.Data.SQLite is the original SQLite database engine and a complete ADO.NET 2.0/3.5 provider all rolled into a single mixed mode assembly. It is a complete drop-in replacement for the original sqlite3.dll (you can even rename it to sqlite3.dll if you’re using it natively).

OK, now we know where this "mixed mode assembly" thing is coming from. It’s an assembly that merges both native and .NET assemblies. To be honest I didn’t even know this was possible before. One more thing learned, now time to see how to get this working in .NET 4.0 – I am expecting it’s strongly attached to the version of the .NET part of the assembly.


As you expect, those kinds of things are solved in configuration also. Here is the related configuration part, also added directly under <configuration> node in app.config:



That was all. It did work afterwards.


Bonus: Using SQLite In Web / ASP.NET On Windows x64 bit


I was scared of the "<startup>" tag that it may not work nicely with ASP.NET, so, I tested the configuration and NHibernate-based test in a new web application and it worked nicely in VS 2010 and IIS.

The Problem

However, the x86 (32 bit) of SQLite (which is the one on NuGet) does not work in IIS on Windows x64 (63 bit) directly. You must enable 32-bit in the application pool.

The error:

Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made to load a program with an incorrect format.


The Solution

As mentioned, you need to set the app pool to support 3-bit assemblies, this, go to IIS, open Application pools:


Right click the application pool you use in your website and pick "Advanced Settings…"


Then change the 32-bit setting…


And now you are done.


On a shared hosting they should be taking care of this already although I’m not sure if you’ll have more severe issues with shared hosting supporting only medium trust security. This always brings problems.


The complete Standard SQLite Configuration

For convenience, here is my complete app.config file:with required config sections.

As mentioned above, you can use those sections in any app.config or web.config files.



I intestinally added the connection string, just to show you how you should be able to do it. Of course the connection string name is up to you and you can choose a file instead of in-memory and I haven’t covered anything about SQLite really. Gogle is your friend if you are interested in more, and you can ask me also as I "might" know!


The Code On Github


As usual, you can browse or download or clone the almost-empty sample application used here from:


If you just want to download it locally I highly advise you get "git" source control and clone the code, however, if you insist on something for direct download, here it is:


I hope this has been helpful to someone trying to get started with this NuGet package.

Share With Friends:

P.S. Please help me out by checking this offer, then look below for a small Thank You.

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.
It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2, ASP.NET 5, and other fullstack developer goodies.

  • Thanks a dozen!

    One question plz, do you know an obfuscator that supports obfuscating a mixed mode assembly?

    • Anonymous


  • Sorry Mohamed for bothering you, but I tried to use the last version of System.Data.Sqlite (v1.0.72.0) rather than ( and it works without setting “useLegacyV2RuntimeActivationPolicy” to true (without mixed mode thing y3ne), can you confirm that in your side? I think there is no need for me now to switch to the free version of “dotConnect for SQLite”.

    Thanks 4 ur time

    • Anonymous

      I’m talking about the NuGet package which is the way I like to get libraries recently.

      But anyway, I now have authority to override the official package. I’ll check the SQLite website and if there is a more updated version, I’ll check why it’s not already on NuGet and whether I can just update the package again.

      Thanks a lot.

      • Anonymous

        Initially I checked and there is a newer version. The NuGet version is the last version in the old website and support forums and is considered “Legacy”.
        I’ll try to check now whether it’s OK if I just publish the newer version to NuGet or no. I “do” have the authority to do it now, but not sure if I own the “decision” though.

  • Good post, Mohamed :)
    Glad u r back to blogging after I noticed that you stopped for a while.