Details of step EntityFramework integrated Sqlite

1, built solutions (this is shown using framework4.0) as shown:

  

2, reference FIG System.Data.SQLite added:

  

3, making sqlite database file

  It is used to establish an Employee table navcat

4, the new database file named: TestSQLite

  It is then copied to the bin / Debug's program as:

 

5, configure connection strings

  <connectionStrings>
    <add name="SQLiteContext" connectionString="Data Source=.\TestSQLite" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>

6, to establish the context

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

 

完成 

  

 

Guess you like

Origin www.cnblogs.com/wjx-blog/p/10953657.html