Example of connecting asp.net EntityFramework 6 to the Sqlite database

Source: Internet
Author: User
Tags sqlite sqlite database connectionstrings

Step 1: Install the System. Data. Sqlite package in NuGet

Step 2: Generate object classes

I tried many times and tried to use Entity Framework Power Tools Beta 4 to connect Sqlite to generate Entity classes, but all failed. The final solution is to create a database with the same structure on SQL Server, after EF Power Tools connects to SQL Server to generate entity classes, remember to modify the database connection string in the configuration file:


<Add name = "testContext" connectionString = "Data Source = F: \ test. db; failifmissing = false" providerName = "System. Data. SQLite. EF6"/>

Note: EntityFramework 6 is used to connect to the Sqlite database provider instead of System. Data. SQLite. EF6, which is explained in the configuration file (App. config.

Step 3: The final content of App. config is as follows:

Reference content
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<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. entityFrameworkSection, EntityFramework, Version = 6.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "requirePermission =" false "/>
</ConfigSections>
<ConnectionStrings>
<Add name = "testContext" connectionString = "Data Source = F: \ test. db; failifmissing = false"
ProviderName = "System. Data. SQLite. EF6"/>
</ConnectionStrings>
<System. data>
<! --
NOTE: The extra "remove" element below is to prevent the design-time
Support components within EF6 from selecting the legacy ADO. NET
Provider for SQLite (I. e. the one without any EF6 support). It
Appears to only consider the first ADO. NET provider in the list
Within the resulting "app. config" or "web. config" file.
-->
<DbProviderFactories>
<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"/>
<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>
<EntityFramework>
<Defaconnecticonnectionfactory type = "System. Data. Entity. Infrastructure. LocalDbConnectionFactory, EntityFramework">
<Parameters>
<Parameter value = "v11.0"/>
</Parameters>
</Defaultonfactory>
<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>
</Configuration>

Step 4: Test the program


Using (testContext context = new testContext ())
{
List <Person> people = context. People. ToList ();
Foreach (Person item in people)
    {
Console. WriteLine ("{0}, {1}", item. Name, item. Age );
    }
}

 

 

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.