Many users would like to be able to use the SQLite database with the Entity Framework 6 EDM Wizard and Designer, but looking at Stack Overflow, they often face problems attempting to do that. This blog post describes the steps required to do that, and gives some troubleshooting advice and tips.
Install the SQLite DDEX provider
UPDATE: This blog post describes how to generate and use an EDMX file with SQLite. I have not tested “Code First from Database”, and “Generate Database from Model” does not work with SQLite. In addtion, Code First Migrations are not supported with SQLite. If you want these additional capabilities in an embedded database, use the SQL Server Compact provider. If you encounter issues with the SQLite provider, suggest you post an issue here.
In order to use the Entity Data Model Wizard to generate an EDMX Model from an existing database, you must be able to connect to the database from Server Explorer in Visual Studio. In order to do that, a so called DDEX provider for the database and Visual Studio version in use must be installed.
For SQLite you must download the provider from http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki - this page is an unreadable mess, sadly.
For Visual Studio 2013, the download you need is named: sqlite-netFx451-setup-bundle-x86-2013-1.0.96.0.exe for the current version of the ADO.NET provider, so 1.0.96 might change to 1.0.97 or higher in the future.
It is important that the DDEXprovider version matches the version of the current SQLite EF6 NuGet package that you use in your project!
Make sure to enable Visual Studio integration and GAC registration during installation.
You can check which version of the DDEX provider you have installed in “Add or Remove Programs”, and also check that no older versions of the DDEX provider are present:
Once installed, you can add a connection to your SQLite database from Server Explorer:
Add the correct SQLite EF6 NuGet package to your project
To demonstrate using the SQLite EF6 provider, let’s go through creating a console app in Visual Studio 2013, that uses the Chinook sample database that we connected to above.
To add the SQLite EF6 provider to your project, select the System.Data.SQLite.EF6 package:
This package will install all other required dependencies, including Entity Framework 6.1.2.
After installation of this package in a project that target .NET 4.5, your packages.config should look like this (notice that with version 1.0.95 of the SQLite provider, only a single package is required);
And the system.data section of your app.config file should be modified to look like this (notice the added <remove> and <add> statements);
<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" />
Important: Now BUILD your project!
You should now be able to add a EDMX based model to your project, using Add New Item. Data, ADO.NET Entity Data Model:
Add some code to the Main method to test that the application also works in runtime:
using (var db = new ChinookEntities())
{
var albums = db.Albums.ToList();
foreach (var item in albums)
{
Console.WriteLine(item.Title);
}
}
Console.ReadKey();
If you are unable to see the connection to the SQLite database we made earlier, here are a couple of tips:
Make sure to build the project and possibly changing the build configuration to x86 has worked for me.
Installing the latest Entity Framework 6.1.2 Visual Studio tools have worked very well for me (the version 6.1.1 Tools are included in Visual Studio 2013 Update 4) – download link in the blog post here. I think it is due to this fix in the Tools.
Hope this helps!
52 comments:
Even when going through all these steps I still cannot get SQLite to work correctly with EF6.
Whenever I try to add migrations it still seems to be looking for the non-EF6 versions.
"Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config."
While (I believe) it should be using: System.Data.SqLite.EF6.SQLiteProviderFactory.
Any ideas?
Please check your machine.config file - DbProviderFactories section, if that turns out to the issue, I will update the blog post. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config - the SQLite Data Provider entry. And I dont think the SQLite provider supports migrations! http://system.data.sqlite.org/index.html/tktview?name=6b5ef4ff7a
Thanks for the post, I followed exactly what you said but I am having a problem when trying to connect to the sqlite database from the server explorer: here is the error message i got once i validate the database file and press OK:
Unable to add data connection
Could not find any resources appropriate fot the specified culture or the neutral culture.
Make sure
"SQLite.Designer.SQLiteDataViewSupport2013.xml" was correctly embedded or linked into assembly "SQLite.Designer" at compile time, or that all the satellite assemblies reqired are loadable and fully signed.
any ideas?
Belachene - looks like a sqlite designer issue. http://system.data.sqlite.org/index.html/tktview/9429dee35bb213db615b13078aeb5c20b4748ca4?plaintext - sure you have the latest designer bits installed, then report at the sqlite site
Thanks Erik;
I've resolved the issue; the reason was that I installed sqlite-netFx451-setup-bundle-x86-2013-1.0.94.0.exe
in top of sqlite-netFx45-setup-bundle-x86-2012-1.0.94.0.exe without unistalling it. I uninstalled it and then reinstalled it and it worked like a charm.
Thanks for the great blog :)
The 1.0.94.1 sqlite dll is version stamped 1.0.94.0. That's no good. Do I need to assembly.load it cos the "null was returned..." bug still exists.
Impy: please post an issue on the Codeplex issue tracker or the sqlite site, I have no idea what your issue is
I went through these steps using VS2013 and everything works fine until I get to the Entity Data Model Wizard where my database is not shown and when I click on the new connection button I don't get an option for SQLite (which is kind of strange as it appeared in the server explored stage earlier.
Simon: Did you install the latest EF 6 VS Tools? Does the SQLite versions in GAC match your DDEX provider version?
After doing all of the tutorial, I tried to get data from the database and I get the following error: "Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config."
Antonio: It is hard to tell what the issue is based on this error message, suggest you provide more info or even better a repro project on Stackoverflow or MSDN
Hi Erik, I have followed all your steps, and I can't see the sqlite connection under the Entity Data Model Wizard. I have installed the EF tools, your SQLite Toolbox and compiled it for x86....what else can I do to make this work?
Michael: See my reply on StackOverflow
ErikEJ, I finally made it work. I had to configure the DbProviderFactories like this:
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
<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>
Antonio: Does that work both runtime and designtime?
ErikEJ, yes. I can send you my App.config if you want.
Basically after many botched attemps, I followed your tutorial and my steps were:
1. Uninstall all older Sqlite connectors
2. Install lastest 2013 connector as specified on your post
3. New project but before trying to create the EDMX I must build the project (even if it's empty) or else it will fail the wizard on the last step. This is also true for the MySQL connector.
4. The connection string must have a full path or in design time, the wizard can't find any tables (basically can't find the file). I think there might be a work around this but I didn't wasted any time checking
5. All done and I got that error that I reported. After some google time and trial and error I realized that you had to add the second DbFactory for the invariant name System.Data.SQLite. I don't know if it uses the other factory for design time and this one for runtime.
Anyway it works. I hope my steps could help others struggling to get EF working with Sqlite.
Antonio: Great info I will add your tips to an update of the blog post!
For future reference, Erik was mentioning this SO post: http://stackoverflow.com/questions/28619432/get-sqlite-to-work-with-vs2013-data-model-designer
BTW, I would love to see a quick tutorial on the connection string, I am not sure how to reference the folder where the DB is located. Also how does this work at deployment time? Do I have to make an installer that creates the DB file or will the application create it for me?
Hi Erik,
Thanks for your great tutorial that allowed me to finally connect to nightmare SQLite DB and create a DBcontext.
When I run VB code: "dbContext.Aircraft.AddRange(ListAircraftFound)" I get the error message:
"Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config."
App.config entries:
-
-
machine.config entry:
Any help to solve my problem will be appreciated,
Thanks
Sfrenchie: i cannot read your config entries, suggest you read all blog comments carefully, and post your issue on msdn or stackoverflow
sfrenchie: I have updated the blog post (new app.config entries)
Thanks, this helped however it didn't completely solve the issue. I am sharing the final solution I found online:
Verify that under Project Properties | References the SQLite dll's are set to copy locally and actually copied to the Bin folder.
VS randomly sets then to False.
Sfrenchie: great info, I will update the blog post with that!
I am using SQLite 1.0.96.0 (installed through nuget) and EF 6.1.2.
The process add the needed configuration to the app.config file, but I don´t have the lines that I have to remove how is said in the blog.
I can compile the application, but I have the error that says "Unable to determine the provider name for provider Factory of type “System.Data.SQLite.SQLiteFactory”. Make sure that the ADO.NET provider is installed or registered in the application config".
I am using visual studio 2013 community and I can create the edmx and all work until I run the application, that I get this error.
Thank you so much.
I am using SQLite 1.0.96.0 (installed through nuget) and EF 6.1.2.
The process add the needed configuration to the app.config file, but I don´t have the lines that I have to remove how is said in the blog.
I can compile the application, but I have the error that says "Unable to determine the provider name for provider Factory of type “System.Data.SQLite.SQLiteFactory”. Make sure that the ADO.NET provider is installed or registered in the application config".
I am using visual studio 2013 community and I can create the edmx and all work until I run the application, that I get this error.
Thank you so much.
Al G: did you add the 2 extra statements to your app.config?
Thank you very much for your post.
I succeeded to configure Entity Framework with SQLITE and everything is working fine.
Except that I don't succeed to refresh my data with the modifications from the database.
I tried to use
var entry = Context.Entry(sl);
entry.Reload();
and
var objectContext = ((IObjectContextAdapter)Context).ObjectContext;
objectContext.Refresh(RefreshMode.StoreWins, Context.Entity);
But I get always the same exception :
System.InvalidOperationException {"No current row"}
Have you an idea of what I am doing wrong ?
Thanks for your help.
Francis: I have no idea what your issue is. Why do you nned to "refresh" data from the database? Everything is local
I got this working fine with "EF Designer from database", but when I try using "Code First from database" I get the following exception at runtime.
"No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. Make sure the provider is registered in the 'entityFramework' section of the application config file."
Kasper: Did you install the version 6.1.3 EF Tools?
I just installed EF Tools 6.1.3, deleted the previously Code First generated entities and context, and added the Data Model again.
Same exception.
I tried with both EF 6.1.2 and 6.1.3.
Kasper: Did you modify your app.config file as suggested in the blog? What are your machine.config entries? Otherwise email me a repro project. Blog comments is not an ideal media for support!
For those who are having the error "Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config." Make sure your project is targeting .net 4.5.1.
@Rui Figueiredo
Thanks for saving me hours of headache. I wasn't targeting .NET 4.5.1...
First of all, thanks a lot ErikEJ!
I followed carefully your steps but without success until I added this line in providers section:
<provider invariantName="System.Data.SQLite"
type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
Thanks again!
hi ErikEJ
you spake new App.config entries in the post upper. Where are they?
Hi
Unknown: not sure what you are refering to...
Hi ErikEJ
i create a project web and the reference dll class library project in which i want to insert EF6 for SQLite.
Now I have the problem for the configuration. Have you any example about this?
HI
Unknown: Suggest you ask in a forum or support site, and provide more details and sample code
Hi ErikEJ,
I am using VS2013 and my project targets .NET Framework 4.0.
Will the DDEX installer for VS2010 work for VS2013?
Many thanks.
S W: No, you must use the VS2013 installer...
Hi ErikEJ!
I have been trying to get SQLite and FE6 worked for a couple days but no success...I am floowing http://www.bricelam.net/2012/10/entity-framework-on-sqlite.html tutorial. My system conf is VS 2015, .NET 4.6, EF6.1.3, SQLite 1.0.99. I have installed sqlite-netFx46-setup-bundle-x64-2015-1.0.99.0 by registring in GAC, added refrences to SQLite libraried into my project manually. Then I installed EF 6 from nuget. As result i got "unable to load System.Data.Sqlite library or it dependencies" (something like that).
Then i installed SQLite libraries from nuget. Finally i was able to run the application without any error. However application can't get any data from data file. Connection string is fine. Also it is not possible to choose sqlite database file when configuring database connection from server explorer.
So i am wondering if my app.conf file is corrupted. There are code snippets:
I will really appreciate if you can have a look.
Radik: I cannot see your config file here, pls email me a repro project.
Thank you so much! this worked great! You sir are awesome!<3
Thank you so much! this worked great! You sir are awesome!<3
Thanks for the great post. I managed to use the EF SQLite successfully. However, I am not able to use insertion functions of EF with SQLite.
I am trying to insert a record like this:
var newperson = new Person() {FirstName = "Someone", LastName = "Somename" };
db.People.Add(newperson);
My person schema has an "INTEGER PRIMARY KEY" Id. As per SQLite specs, this acts as the ROWID that SQLite engine will assign automatically. When using plain SQL this works.
The problem is that the ADD function generates SQL that sets ID=0. This prevents the SQLite engine from auto-generating a ROWID value for Id field because it is explicitly set. Any idea how I can make the addition without explicitly managing the Primary key ID generation by myself?
Moataz: Mark the key on your class as StoreGenerated...
Thanks Erik! that worked. Now I am not sure if I should use AUTOINCREMENT to avoid data issues in case of ROWID reuse for my table primary key. As I understand, SQLite may re-use deleted records ROWID values in future records if the key is not set to AUTOINCREMENT. It is slightly off-topic but if you can share some insight that would be great :)
Moataz: I am not really a SQLite expert, I can only Refer you to the official SQLite douementation at sqlite.org
Changing the build target to x86 worked for me. THANK YOU!!
Cannot get this work on VS 2017? Different producedure?
Unknown: see my list of tips here for VS 2017: https://github.com/ErikEJ/SqlCeToolbox/wiki/EF6-workflow-with-SQLite-DDEX-provider
Post a Comment