EntityFramework集成Sqlite的详细步骤
发布时间:2020-12-12 18:56:46 所属栏目:百科 来源:网络整理
导读:1、建解决方案(本示使用的是 framework4.0)如图: 2、添加?System.Data.SQLite引用 如图: 3、制作sqlite数据库文件 使用的是navcat 建立一张Employee表 4、把新建的数据库文件起名为:TestSQLite 然后拷贝到程序的bin/Debug里,如图: 5、配置链接字符串
1、建解决方案(本示使用的是 framework4.0)如图:
2、添加?System.Data.SQLite引用 如图:
3、制作sqlite数据库文件 使用的是navcat 建立一张Employee表 4、把新建的数据库文件起名为:TestSQLite 然后拷贝到程序的bin/Debug里,如图:
5、配置链接字符串 <connectionStrings> <add name="SQLiteContext" connectionString="Data Source=.TestSQLite" providerName="System.Data.SQLite.EF6" /> </connectionStrings> 6、建立上下文 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; using System.Data.SQLite; using System.Linq; using System.Text; namespace ConsoleApp8 { public class SQLiteTest { /// <summary> /// 此属性用于指定配置类 /// </summary> //[DbConfigurationType(typeof(MyConfiguration))] public class SQLiteContext : DbContext { /// <summary> /// 指定连接字符串 /// </summary> /// <param name="filename"></param> public SQLiteContext(string filename) : base(new SQLiteConnection() { ConnectionString = new SQLiteConnectionStringBuilder() { DataSource = filename,ForeignKeys = true } .ConnectionString },true) { } /// <summary> /// 生成数据库用,不需要生成的话可以注释掉 /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(DbModelBuilder modelBuilder) { } public DbSet<Employee> Employees { get; set; } } [Table("Employee")] public class Employee { public int EmployeeID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } } } ?7、测试 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp8 { class Program { static void Main(string[] args) { SQLiteTest.SQLiteContext context; context = new SQLiteTest.SQLiteContext("TestSQLite"); var empList = context.Employees.OrderBy(c => c.FirstName).ToList(); } } } 错误1 遇到错误如下: System.InvalidOperationException:“No Entity Framework provider found for the ADO.NET provider with invariant name ‘System.Data.SQLite‘. Make sure the provider is registered in the ‘entityFramework‘ section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.” ? 解决方法: 去掉app.config的如下代码: 把红线标的部分去掉,新的app.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> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </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> <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> ? 错误2? 如果遇到找不着“SQLite.Interop.dll” 的错误 解决方法: 去当前解决方法的nugit包里的路径里复制出来这两个文件夹,拷贝到 bin/debug里,如图: ? 完成? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |