winform SQLite vs2019 EF ADO.net实体数据模型

VisualStudio2019  C#  EntityFramework6  SQLite的环境配置

我最近的开发都使用微软的EF6,这种把数据库关系到对象上的方式操作数据非常简单,不需要去记烦人的SQL,并且在对象操作中就实现了数据关联到数据库中,这会使代码更少更优雅。我喜欢用EntityFramework,很多需求,就开发一个小工具,也用不上专门的数据库系统,保存到本地的数据库文件中就可以了,SQLite是很好的选择。

ErikEJ  EntityFramework的专家,为我们带来了SQLite Toolbox来实现vs2015,vs2017,vs2019中使用C#,EntityFramework,  ADO.net实体数据模型,SQLite的技术组合。

以WINFORM为例,按下面的四步,我们在项目中添加“ADO.net实体数据模型”,关联到SQLite文件数据库,实现了EF6的LINQ语法操作文件数据库。

---------2022.2.17----安装日记------------

1、安装SQLite and SQL Server Compact Toolbox v4.8.740.vsix

链接:https://pan.baidu.com/s/1AryttkJCyxtguUfx9isKSw?pwd=dls3 
提取码:dls3

2、sqlite-netFx46-setup-bundle-x86-2015-1.0.115.5.exe 

链接:https://pan.baidu.com/s/1bNSZBnUcyiqiHB9dph9A0A?pwd=fu4w 
提取码:fu4w

3、PM> Install-Package System.Data.SQLite

      SQLite相关类库会自动更新到最新版

4、打开原来的程序发现实体模型中没有表,原来文件数据库需要指定全路径地址: 

 

---------2021.10.21----安装日记------------

1、安装SQLite and SQL Server Compact Toolbox v4.8.738.vsix

2、sqlite-netFx46-setup-bundle-x86-2015-1.0.115.0.exe 

这个文件下载地址有一点调整:System.Data.SQLite: Downloads Page - Unsupported

3、PM> Install-Package System.Data.SQLite

----------下文是作者ErikEJ的详细安装说明:----------

https://github.com/ErikEJ/SqlCeToolbox/wiki/EF6-workflow-with-SQLite-DDEX-provider

需要配置的步骤如下:

一、Install latest Toolbox

Once per Visual Studio edition (daily build at https://github.com/ErikEJ/SqlCeToolbox/wiki/Release-notes )

二、Install SQLite in GAC

Once per machine. Download the latest sqlite-netFx46-setup-bundle-x86-2015-1.0.xxx.0.exe (from System.Data.SQLite: Downloads Page)

Select "Full Installation"

Select: Install the assemblies into the global assembly cache - Install VS designer components

sqliteddex

Restart Visual Studio

Verify that the EF6 provider is installed in GAC from the Toolbox "About" dialog:

sqliteddex4

sqliteddex

If the EF6 provider is not in GAC, this may be due to an invalid entry in machine.config, located in the C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config folder. The only SQLite related entry should look like this, with this exact version number:

<system.data>
    <DbProviderFactories> 
        <add name="SQLite Data Provider" 
             invariant="System.Data.SQLite.EF6" 
             description=".NET Framework Data Provider for SQLite" 
             type="System.Data.SQLite.EF6.SQLiteProviderFactory, 
                    System.Data.SQLite.EF6, 
                    Version=1.0.113.0, 
                    Culture=neutral, 
                    PublicKeyToken=db937bc2d44ff139" 
        />  
    </DbProviderFactories> 
</system.data>

Some users report that adding this to app.config solves some runtime issues.

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

三、Install System.Data.Sqlite NuGet package

Install using Package Manager Console or NuGet Manager in each project.

PM> Install-Package System.Data.SQLite

Make sure to install the same version as the tools package above.

Build project!

Packages.config should look like this after install:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.2.0" targetFramework="net461" />
  <package id="System.Data.SQLite" version="1.0.113.0" targetFramework="net461" />
  <package id="System.Data.SQLite.Core" version="1.0.113.0" targetFramework="net461" />
  <package id="System.Data.SQLite.EF6" version="1.0.113.0" targetFramework="net461" />
  <package id="System.Data.SQLite.Linq" version="1.0.113.0" targetFramework="net461" />
</packages>

App.config should look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <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" />
    <remove invariant="System.Data.SQLite" />
   </DbProviderFactories>
  </system.data>
</configuration>

四、Run Entity Data Model Wizard

Add, New Item, Data, ADO.NET Entity Data Model. Choose "EF Designer from Database" or "Code First from Database"

Use "SQLite Provider (Simple for EF6 by ErikEJ)" when creating a connection to your SQLite database file. Enter the full path to your database file in Data Source.

sqliteddex3

A reader of this wiki post has provided some additional tips here

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘欣的博客

你将成为第一个打赏博主的人!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值