Let EF fly for a while: How to Use Entity Framework 6 to connect to the Sqlite database, entitysqlite

Source: Internet
Author: User

Let EF fly for a while: How to Use Entity Framework 6 to connect to the Sqlite database, entitysqlite
Obtain Sqlite

1. You can use the NuGet package to obtain it. It will also automatically download EF6.

2. Download the corresponding version on the Sqlite Official Website: http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

Note that each. net framework has two versions, one with the bundle word and the other. One installed DLL contains SQLite. Interop. dll, while the other does not. If you report

If you fail to load SQLite. Interop. dll, copy SQLite. Interop. dll from the installation file to the binfile. Or there is a corresponding program in packages \ System. Data. SQLite. Core.1.0.94.0 \ build downloaded by NuGet.

Sample Code

Model. cs

Public class Person {public Int64 Id {get; set;} // be sure to use Int64 public string FirstName {get; set;} public string LastName {get; set ;}} public class MyContext: DbContext {public DbSet <Person> Persons {get; set;} public MyContext (): base ("SqliteTest "){}}

Program. cs

 static void Main(string[] args)        {            MyContext context = new MyContext();            var empList = context.Persons.OrderBy(c => c.FirstName).ToList();            Console.WriteLine(empList.Count);            Person people = new Person()            {                FirstName = "Hello",                LastName = "World"            };            context.Persons.Add(people);            context.SaveChanges();            Console.ReadLine();        }

The sample code is simple. You can use EF to add and view the Person table.

Configure the config file

If you use NuGet to obtain Sqlite, related information is automatically configured in config.

<? 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 =" SqliteTest "connectionString =" data source = SqliteTest. db "providerName =" System. data. SQLite. EF6 "/> </connectionStrings> <startup> <supportedRuntime version =" v4.0 "sku = ". NETFramework, Version = v4.5 "/> </startup> <system. data> <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> <defaconnectionfactory 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>View Code

The data connection string is:

   <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />

Note that the provided assembly is System. Data. SQLite. EF6.

However, this configuration is still incorrect.

If you run the program at this time, an error is reported:

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

Or Chinese error message:

The Entity Framework provider with the fixed name "System. Data. SQLite" is not found. Make sure that the provider is registered in the "entityFramework" section of the application configuration file.

EF does not find the dll that provides System. Data. SQLite. SQLiteFactory. Let's look at the entityFramework node in config:

  <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" />    </providers>  </entityFramework>

There are System. Data. SQLite. EF6 and System. Data. SqlClient. There is indeed no provider named System. Data. SQLite. Here I have never understood why sqlite will find the provider named System. Data. SQLite, because the provider we configured in the connection string is also System. Data. SQLite. EF6.

Then we add a provider named System. Data. SQLite in the configuration node of EF, but the type is still System. Data. SQLite. EF6. Final Configuration

The red part is where the configuration changes.

Run the program here.

 

Note:

1. Configure the connection string.

You can specify an absolute address or a relative address for a data connection string. Like my data source = SqliteTest. db, then SqliteTest. db needs to be in the Bin folder. If it is a web program, you can use Data Source = | DataDirectory | \ SqliteTest. db to be configured in the App_Data file flat.

2. if the name of the table in the database is not specified, all SQL tables generated by EF are expressed in the plural. Just like in my program, the entity name is Person, but the table name searched by EF will be People. Therefore, the table name defined in the database is "People.

3. CodeFirst mode is not supported. You must first design the table structure of Sqlite.

 

Sample Code (the packages file is too large, so it is deleted): SqliteLinqTest.zip


How does entity framework connect to the sqlserver2005 database?

For more information, see msdn.microsoft.com/...6.aspx.
 
Does Entity Framework use a custom model and the database cannot be connected?

Public class StoreContent: DbContext
{
Public DbSet <Order> Orders {set; get ;}
...

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.