Some of the configuration of the Entity framework when using SQLite

Source: Internet
Author: User
Tags compact microsoft sql server sqlite

Some time ago tried to use the entity Framework for SQLite environment, found some potholes, record.

At the same time, try configuring multiple databases, including SQLite, SQL Server, SQL Server LocalDB, SQL Server Compact.

I built the demo project structure and the packages installed through NuGet:

 

EFDemo.MultipleDB.UI references the Efdemo.mutipledb project.

1. Exceptions encountered 1

The Entity Framework provider type ' System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6 ' registered The application config file for the ADO provider with invariant name ' System.Data.SQLite.EF6 ' could is not loaded. Make sure, the assembly-qualified name is used and that the assembly are available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

I refer to the SQLite-related DLLs (see), but found that the generated files are not automatically copied in the generated file under EFDemo.MultipleDB.UI.

2.

So I copied these manually.

2. Exceptions encountered 2

Just now the exception was gone, and there was an exception.

The Entity Framework provider type ' System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver ' Registered in the application config file for the ADO provider with invariant name ' System.Data.SqlClient ' could not B E loaded. Make sure, the assembly-qualified name is used and that the assembly are available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

This time in the code to add a paragraph, do not call, just let it exist in the code:

        <summary>        ///solve the problem that provider cannot be loaded automatically        ///(no need to call, put here)///        </summary>        private static void Fixprovidersnotautoloadproblem ()         {            var _ = typeof (System.Data.SQLite.EF6.SQLiteProviderFactory);            var __ = typeof (System.Data.Entity.SqlServer.SqlProviderServices);            var ___ = typeof (System.Data.Entity.SqlServerCompact.SqlCeProviderServices);        }

3. Exceptions encountered 3

Using the above method, the above exception is gone, and a new one is coming.

Unable to determine the provider name for provider factory of type ' System.Data.SQLite.SQLiteFactory '. Make sure that the ADO provider is installed or registered in the application config.

The packages for SQLite are installed through NuGet, but NuGet doesn't seem to be helping to build a complete configuration, so it needs to be built on its own.

The paste is complete, note that the red is added:

<system.data> <DbProviderFactories><remove invariant= "System.Data.SQLite"/> <add name= "SQLite Data Provider" INV Ariant= "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)" in Variant= "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.sqlserverce.4.0 "/> <add name=" Microsoft SQL Server Compact Data Provider 4.0 "invariant=" System.da Ta. sqlserverce.4.0 "description=". NET Framework Data Provider for Microsoft SQL Server Compact "Type=" System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, version=4.0.0.0, Culture=neutral, Publickeytoken=89845dcd8080cc91 "/> </DbProviderFactories> </system.data> <entityFramework> &      Lt;defaultconnectionfactory type= "System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework" > <parameters> <parameter value= "system.data.sqlserverce.4.0"/> </Parameters> </defaultConnectionFactory> <providers>       <provider invariantname= "System.Data.SQLite" type= "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6 "/><provider invariantname= "System.Data.SQLite.EF6" type= "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6 "/> <provider invariantname=" System.Data.SqlClient "type=" System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver "/> <provider invariantname=" system.data.sqlserverce.4.0 "Type=" System.Data.Entity.SqlServerCompact.SqlCeProviderServices, Entityframework.sqlservercompact "/> </providers> </entityFramework>

4. Although code first is used, SYSTEM.DATA.SQLITE.EF6 does not help us to automatically generate the table structure of the database and needs to be created manually. (Of course, you can find any other migration that support SQLite's EF Provider)

OK, these pits are filled in.

5. Set DataDirectory

The connection string for my SQLite is written like this:

<add name= "Bloggingcontext_sqlite" connectionstring= "Data source=| Datadirectory|\blogging_sqlite.db "providername=" System.Data.SQLite.EF6 "/>

For ease of development, I put the database file inside the project, like this:

How do I tell the connection string datadirectory here?

        private static void Setdatadir ()         {            DirectoryInfo baseDir = new DirectoryInfo ( AppDomain.CurrentDomain.BaseDirectory);            string data_dir = Basedir.fullname;            if (baseDir.Name.ToLower () = = "Debug" | | baseDir.Name.ToLower () = = "Release")                && ( BaseDir.Parent.Name.ToLower () = = "Bin")            {                Data_dir = Path.Combine (BaseDir.Parent.Parent.FullName, "App_ Data ");            }                        AppDomain.CurrentDomain.SetData ("DataDirectory", Data_dir);        }

Called when the program is initialized.

Report:

The complete code here

Some of the configuration of the Entity framework when using SQLite

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.