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

posted @   霍旭东  阅读(765)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示