I am trying to get System.Data.SQLite to work with Entity Framework 6 using the Code First approach in my WPF C # .NET project (4.5.2).
I looked through and tried to follow the instructions in the following places (among other things), but they seem deprecated for different dbms and not for First code or any combination above.
https://msdn.microsoft.com/en-us/data/jj592674
https://msdn.microsoft.com/en-us/data/jj592674.aspx
http://nullskull.com/a/10476742/sqlite-in-wpf-with-entity-framework-6.aspx
Entity Framework 6 + SQLite
I think I finally installed the App.config file correctly, as shown below:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <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" /> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework> <connectionStrings> <add name="MyDatabase" connectionString="Data Source=.\DataFile\myDatabase.sqlite" providerName="System.Data.SQLite" /> </connectionStrings> <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> </configuration>
The definition of My DbContext is as follows:
public class MyDataContext : DbContext { public MyDataContext() : base("name=MyDatabase") { } public DbSet<DataItem> DataItems { get; set; } }
My DataItem class is as follows:
public class DataItem { [Key] public int MyInt { get; set; } public string MyString { get; set; } }
And I am trying to call the context as follows:
using (var db = new MyDataContext()) { DataItem item = new DataItem { MyInt = 19, MyString = "nineteen" }; db.DataItems.Add(item); try { db.SaveChanges(); } catch (Exception ex) { var x = ex.InnerException; Debug.WriteLine("Inner Exception: {0}", x); throw; } }
As I expected, when I create an instance of MyDataContext , the myDatabase.sqlite file is created in my bin\debug\DataFile . However, when I try to call db.SaveChanges() , an exception is thrown.
InnerException for this exception 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: DataItems 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: DataItems
When I look in the DataFile directory, myDatabase.sqlite is zero. This tells me that although DbContext found the correct file name for the new database file from the app.config file, it did not create its first lines of code in the database, as intended.
Is there something obvious I'm missing here? All examples assume that this (extremely critical) step works, and everything in EF6 results from a working, properly configured database.
Thanks in advance for any help you can offer me.