Let EntityFramework6 support SQLite

Source: Internet
Author: User
Tags sqlite database connectionstrings

Recently made a graduate design for a friend's child. With ASP. MVC5 + EntityFramework6 + SQL Server 2008.

The result is good, friend said can not database, directly run? Immediately let me very tangled, do not need to install a database but can run, can only consider a single file database, such as Access or SQLite.

Accessing the data found that access could not support EntityFramework's operation, just consider SQLite.

The SQLite method publishes an assembly supported by ENTITYFRAMEWORK6 and executes the following command in the project:

Install-package System.data.sqlite.ef6install-package System.Data.SQLite.Linq

Three references will appear after installation:

The following configuration is then configured in Web. config:

  <configSections> <!--For more information on Entity Framework configuration, visit http://go.microsoft.com/ fwlink/? linkid=237468--<section name= "EntityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFramewo Rksection, EntityFramework, version=6.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirePermission=" False "/> </configSections> <connectionStrings> <add name=" educationstrings "Providername=" Syste M.data.sqlite.ef6 "connectionstring=" Data source=education.db; Pooling=true "/> </connectionStrings><entityFramework> <providers> <provider Invariantnam E= "System.Data.SQLite.EF6" 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> <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=" Sys Tem. Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6 "/> </DbProviderFactories> </system.data >

After this configuration, the exception information is found to be thrown as follows:

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.

I guess I didn't find a pipeline factory method that supports ADO. In StackOverflow search discovery, you need to add a configuration to System.Data.SQLite.SQLiteFactory in Web. config.

The providers child nodes in the EntityFramework node are added with the following configuration:

<provider invariantname= "System.Data.SQLite" type= "System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6 "/>

Next, the DbProviderFactories child node of the System.Data node is configured as follows:

<add name= "SQLite data Provider" invariant= "System.Data.SQLite" description= ". Net Framework Data Provider for SQLite" Type= "System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>

This is finally supported by ADO, but the following exceptions will be thrown:

Unable to open database file

Probably cannot open the database file, the data found that the program read the SQLite database need to use a physical absolute path, the solution has two, one is to specify the database configuration file in the code, one is to specify a similar variable value in Web. config, as follows:

<add name= "educationstrings" providername= "System.Data.SQLite.EF6" connectionstring= "Data source=| datadirectory|\education.db; Pooling=true "/>

Where DataDirectory refers to the directory of the database, which corresponds to the App_Data folder under the ASP, so you need to put the database into that directory.

Another exception is thrown when the modification is complete:

SQL logic error or missing Databaseno such table:articles

Probably said not to find the table articles, my project with the Codefirst mode, it appears that SQLite does not support Codefirst mode, can only manually build the table. If there are fewer tables or fewer fields, if you have a large number of tables, it would be nice to manually build the table, if you could import a SQL Server 2008 database into SQLite.

After Google, finally found a tool sqlconverter, the tool can be SQL Server tables and tables in the database into SQLite.

After conversion, it is found that the primary key in SQLite can only be an integer type, corresponding to C # int64. But my model is int32, do not know is the SQLite stipulation or the tool conversion has the problem.

The complete Web. config configuration is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><!--For more information about how to configure an ASP. NET application, go to http://go.microsoft.com/fwlink/? linkid=301880--><configuration> <configSections> <!--for more information on Entity Framework con figuration, visit http://go.microsoft.com/fwlink/? linkid=237468--<section name= "EntityFramework" type= "System.Data.Entity.Internal.ConfigFile.EntityFramewo Rksection, EntityFramework, version=6.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "requirePermission=" False "/> </configSections> <appSettings> <add key=" webpages:version "value=" 3.0.0.0 "/> & Lt;add key= "webpages:enabled" value= "false"/> <add key= "clientvalidationenabled" value= "true"/> <add K ey= "unobtrusivejavascriptenabled" value= "true"/> </appSettings> <connectionStrings> <!--<add N Ame= "Educationstrings" providername= "System.Data.SqlClient" connectionstring= "Data source=.; User=sa; Password=123456;initial catalog=educationdb;integrated security=true "/>--> <add name=" EducationStrings "ProviderName=" System.Data.SQLite.EF6 "connectionstring=" Data source=| datadirectory|\education.db;  Pooling=true "/> </connectionStrings> <system.web> <compilation debug=" True "targetframework=" 4.5 " /> 

This will finally work. Attached code and tools.

Code Download:

http://download.csdn.net/detail/lifeilin6671/7837447

Conversion Tool Download:

http://download.csdn.net/detail/lifeilin6671/7837465

Let EntityFramework6 support 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.