EF With SQLite

EF 虽说官方声称支持SQLite,但实际用起来还真没有SQLSever好使。

 

  • 不支持真正的CodeFirst,需要先建表结构。
  • 不支支持Migration
  • 需要修改App.config 文件

 

安装

System.Data.SQLite (x86/x64) NuGet package

 

配置app.config

 

因为Nuget下载后配置的App.config文件汇报错,所以还需要修改下:

<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" />

     <!--新增部分-->

        <provider invariantName="System.Data.SQLite"

                 type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

   </providers>

  </entityFramework>

 

<system.data>

    <!--

        NOTE: The extra "remove" element below is to prevent the design-time

              support components within EF6 from selecting the legacy ADO.NET

              provider for SQLite (i.e. the one without any EF6 support).  It

              appears to only consider the first ADO.NET provider in the list

              within the resulting "app.config" or "web.config" file.

    -->

    <DbProviderFactories>

      <DbProviderFactories>

        <remove invariant="System.Data.SQLite" />

        <!--新增部分-->

        <add name="SQLite Data Provider"

             invariant="System.Data.SQLite"

             description=".Net Framework Data Provider for SQLite"

             type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />

        <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>

    </DbProviderFactories>

  </system.data>

 

更多细节查看:

 

http://hintdesk.com/sqlite-with-entity-framework-code-first-and-migration/

 

https://stackoverflow.com/questions/21757843/system-data-sqlite-1-0-91-0-and-ef6-0-2?lq=1

Unfortunately, the EF6 provider implementation in System.Data.SQLite.EF6 doesn't currently support creating tables.

https://stackoverflow.com/questions/22174212/entity-framework-6-with-sqlite-3-code-first-wont-create-tables

Sqlite with Entity Framework Code First and Migration

你可能感兴趣的:(sqlite)