對於EF支持Sqlite數據庫映射,網上彷佛說得都不是很清楚,本身研究了會兒,如今給你們分享下~sql
所使用的庫版本數據庫
EntityFramework.6.1.0ide
SQLite.1.0.92.0測試
以上兩個庫能夠經過nuget下載,具體下載方式不在此說明.ui
引用結構spa
配置code
App.config配置sqlite
<?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" /> </configSections> <entityFramework> <providers> <provider invariantName="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> <connectionStrings> <add name="NorthwindContext" connectionString="Data Source=Northwind.sl3" providerName="System.Data.SQLite.EF6" /> </connectionStrings> <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="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" /> </DbProviderFactories> </system.data> </configuration>
這裏provider配置只須要System.Data.SQLite.EF6就行.xml
代碼實現blog
繼承DbContext,並定義實體
public class NorthwindContext : DbContext { public DbSet<Employee> Employees { get; set; } } public class Employee { public int EmployeeID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
測試
class Program { static void Main(string[] args) { NorthwindContext context = new NorthwindContext(); var empList = context.Employees.OrderBy(c => c.FirstName).ToList(); Console.WriteLine(empList.Count); Console.ReadLine(); } }
能夠正確執行Linq.
參考:
http://hintdesk.com/sqlite-with-entity-framework-code-first-and-migration/
http://www.nullskull.com/a/10476742/sqlite-in-wpf-with-entity-framework-6.aspx
但願以上信息對你有用~