對於EF支持Sqlite數據庫映射,網上似乎說得都不是很清楚,自己研究了會兒,現在給大家分享下~
所使用的庫版本
EntityFramework.6.1.0
SQLite.1.0.92.0
以上兩個庫可以通過nuget下載,具體下載方式不在此說明.
引用結構
配置
App.config配置
<?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就行.
代碼實現
繼承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
希望以上信息對你有用~

