Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example using System.Data.SQLite with Entity Framework 6

I am trying to get a simple code first example to work in a console app using SQLite and EF6, however I am running into multiple errors: I created a new console project in VS 2015. Then install EF (6.1.3) and System.Data.SQLite (1.0.102) via NuGet.

Try to run a simple program:

namespace SQLiteConsole1 {     class Person     {         public int Id { get; set; }         public string Name { get; set; }     }      class MyContext : DbContext     {         public DbSet<Person> Persons { get; set; }     }      class Program     {         static void Main(string[] args)         {             using (var db = new MyContext())             {                 var person = new Person() { Name = "John" };                 db.Persons.Add(person);                 db.SaveChanges();             }         }     } } 

This is what my App.Config looks like this:

  <connectionStrings>     <add name="MyContext" connectionString="Data Source=C:\Temp\Test.sqlite" providerName="System.Data.SQLite" />   </connectionStrings>   <entityFramework>     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />     <providers>       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />       <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />     </providers>   </entityFramework>   <system.data>     <DbProviderFactories>       <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" />     <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" /></DbProviderFactories>   </system.data> 

When I first run this program I get the following error:

Unhandled Exception: System.InvalidOperationException: 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."

So I change <provider invariantName="System.Data.SQLite.EF6" to <provider invariantName="System.Data.SQLite", then I get this error:

Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. System.Data.SQLite.SQLiteException: SQL logic error or missing database no such table: People

What needs to be changed to get this simple example working?

like image 312
RaelB Avatar asked Jul 24 '16 21:07

RaelB


People also ask

Can I use Entity Framework with SQLite?

This database provider allows Entity Framework Core to be used with SQLite. The provider is maintained as part of the Entity Framework Core project.

How do I add SQLite to Visual Studio?

Getting Started with SQLite from a .Open Visual Studio, select new project, and, in Visual C#, select “Console Application” and provide the name as SQLiteDemo. Click OK. To connect SQLite with C#, we need drivers. Install all required SQLite resources from the NuGet package, as pictured in Figure 1.

Does SQLite support .NET core?

SQLite is a self-contained and embedded SQL database engine. In . NET Core, Entity Framework Core provides APIs to work with SQLite.

What is SQLite provider?

Entity Framework Extensions EF6 - SQLite Provider. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It is the most widely deployed SQL database engine and the source code for SQLite is in the public domain.


1 Answers

A similar question is asked over here: Entity Framework 6 with SQLite 3 Code First - Won't create tables

kjbartel gives very useful explanation that table creation is not supported by the EF SQLite Driver.

Also see https://github.com/msallin/SQLiteCodeFirst, which provides an excellent solution. I installed the SQLite.CodeFirst NuGet package, and added the below code, then the app works fine:

    class MyContext : DbContext     {         protected override void OnModelCreating(DbModelBuilder modelBuilder)         {             var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyContext>(modelBuilder);             Database.SetInitializer(sqliteConnectionInitializer);         }         public DbSet<Person> Persons { get; set; }     } 
like image 140
RaelB Avatar answered Sep 18 '22 12:09

RaelB