EntityFramework集成Sqlite的详细步骤

EntityFramework集成Sqlite的详细步骤

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里,如图:

完成

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部