I am doing the simplest console project to see how to work with SQLite.

I add a System.Data.SQLite (x86 / x64) package from Nuget, which automatically pulls EF6 and Linq for it. I create the simplest code for testing.

public class Human { [Key] public Int64 ID { get; set; } public String Name { get; set; } } public class HContext : DbContext { public HContext() : base("mydb") { } public DbSet<Human> Humans { get; set; } } class Program { static void Main(string[] args) { HContext ctx = new HContext(); ctx.Humans.Add(new Human() { ID = 1, Name = "Patrik" }); ctx.SaveChanges(); } } 

Great, everything works and starts, but the database is created in the% UserProfile% directory. I add connectionString to the configuration file, so that the database is created in the application directory and I get an error.

 <connectionStrings> <add name="mydb" connectionString="data source =.\mydb.sqlite;" providerName="System.Data.SqlClient"/> </connectionStrings> 

Additional information: This can be caused by the Entity Framework using an incorrect connection string. The connection string is correct.

I tried other options, the effect is the same, search engines are silent as partisans. In this regard, the question of how to register connectionString?


UPD

Also in the configuration file is System.Data.SQLite.EF6. But when I try to connect it in connectionString, I get this error.

Additional information: System.Data.SQLite.SQLiteFactory. Make sure that the ADO.NET provider is installed or registered in the application config.

  • Or you can answer your own question and then make your own decision. - Pavel Mayorov
  • @PavelMayorov did not help, but it led to the right thoughts)) The answer is in the body of the question, and the question itself will be closed through your answer. - Alex Krass
  • one
    Alex Krass, do not answer in the body of the question. This site is not accepted. - Pavel Mayorov
  • @PavelMayorov, okay, translated into a separate answer. Although as for me in the body of the question would also look good. - Alex Krass

2 answers 2

You need to specify a provider for SQLite instead of System.Data.SqlClient . System.Data.SqlClient is a provider for MS SQL Server.

Most likely, the provider you need will be called System.Data.SQLite . Specify in your own configuration file - the package you installed should have added your provider’s registration to it.


If you still get an error, see InnerException . EF loves to hide the cause of the error somewhere inside, sometimes under several "layers" of external exceptions.

  • Yes, after the second day of the war with him the brains are already going. By default, there is System.Data.SQLite.EF6, but it also gives an error if I specify it as a config. Already, I simply don’t have enough strength to think, maybe I missed SQLite myself somewhere? Laid out the full config sheet. - Alex Krass
  • Everything, understood, thanks. There, registration with the SQLite EF provider takes place via SQLite.EF6, which I never expected. That fought in the error all this time. - Alex Krass

It was necessary to connect via System.Data.SQLite.EF6, while still registering System.Data.SQLite via System.Data.SQLite.EF6 in the EF itself: <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

The final soared config looks like this:

 <?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" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <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> </entityFramework> <system.data> <DbProviderFactories> <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" /> <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" /> </DbProviderFactories> </system.data> <connectionStrings> <add name="mydb" connectionString="data source =.\mydb.sqlite" providerName="System.Data.SQLite.EF6"/> </connectionStrings> </configuration>