获取Sqlite

1.可以用NuGet程序包来获取,它也会自动下载EF6

2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

注意这里面每个.net framework都有两个版本,一个带有bundle字眼,一个没有。一个安装的DLL里面包含SQLite.Interop.dll,而另一个没有。如果你运行代码的时候报

“无法加载SQLite.Interop.dll”的错误,则将安装文件中的SQLite.Interop.dll拷贝到Bin文件中即可。或是在NuGet下载的packages\System.Data.SQLite.Core.1.0.94.0\build中也有对应的程序。

示例代码

Model.cs

  1. public class Person
  2. {
  3. public Int64 Id { get; set; } //注意要用Int64
  4. public string FirstName { get; set; }
  5. public string LastName { get; set; }
  6. }
  7.  
  8. public class MyContext : DbContext
  9. {
  10. public DbSet<Person> Persons { get; set; }
  11.  
  12. public MyContext()
  13. : base("SqliteTest")
  14. {
  15.  
  16. }
  17. }

Program.cs

  1. static void Main(string[] args)
  2. {
  3. MyContext context = new MyContext();
  4. var empList = context.Persons.OrderBy(c => c.FirstName).ToList();
  5. Console.WriteLine(empList.Count);
  6.  
  7. Person people = new Person()
  8. {
  9. FirstName = "Hello",
  10. LastName = "World"
  11. };
  12. context.Persons.Add(people);
  13. context.SaveChanges();
  14. Console.ReadLine();
  15. }

示例代码很简单,就是用EF对Person表进行新增与查看。

配置config文件

如果你是用NuGet获取Sqlite,会自动在config中配置一些相关的信息。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <configSections>
  4. <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  5. <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  6. </configSections>
  7. <connectionStrings>
  8. <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />
  9. </connectionStrings>
  10. <startup>
  11. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  12. </startup>
  13. <system.data>
  14. <DbProviderFactories>
  15. <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
  16. <remove invariant="System.Data.SQLite" />
  17. <remove invariant="System.Data.SQLite.EF6" />
  18. <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" />
  19. </DbProviderFactories>
  20. </system.data>
  21. <entityFramework>
  22. <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  23. <parameters>
  24. <parameter value="v11.0" />
  25. </parameters>
  26. </defaultConnectionFactory>
  27. <providers>
  28. <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  29. <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
  30. </providers>
  31. </entityFramework>
  32. </configuration>

其中数据连接串是:

  1. <add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />

注意提供程序集是System.Data.SQLite.EF6。

但是这个配仍然是错误的。

如果此时运行程序,会报错:

Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

或中文错误信息:

未找到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序。

意思是EF没有找到提供System.Data.SQLite.SQLiteFactory的dll,我们看看现在config中的entityFramework节点:

  1. <entityFramework>
  2. <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  3. <parameters>
  4. <parameter value="v11.0" />
  5. </parameters>
  6. </defaultConnectionFactory>
  7. <providers>
  8. <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  9. <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
  10. </providers>
  11. </entityFramework>

有System.Data.SQLite.EF6与System.Data.SqlClient,确实没有名称为System.Data.SQLite的提供程序。这里我一直不明白为什么sqlite会去找名称为System.Data.SQLite的提供程序,因为我们在连接串中配置的provider也是System.Data.SQLite.EF6。

那我们就在EF的配置节点中增加一个名为System.Data.SQLite的provider,但type仍然是System.Data.SQLite.EF6。最终的配置如图:

红色部分是配置有变化的地方。

这里再运行程序就可以了。

注意:

1.连接串的配置。

数据连接串可以指定绝对地址,也可以指定相对地址。像我的data source=SqliteTest.db,则SqliteTest.db要在Bin文件夹中,如果是web程序可以通过Data Source=|DataDirectory|\SqliteTest.db来配置在App_Data文件平中。

2.如果没有指定数据库中的表文件名,EF生成的SQL表都是用复数表示。就像我的程序中实体名是Person,但EF去查找的表名会是People。所以在数据库中定义的表名是People。

3.不支持CodeFirst模式,您需要自己先设计好Sqlite的表结构。

示例代码(packages文件太大,所以删除了):SqliteLinqTest.zip

让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库的更多相关文章

  1. 如何用Entity Framework 6 连接Sqlite数据库[转]

    获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...

  2. UWP: 在 UWP 中使用 Entity Framework Core 操作 SQLite 数据库

    在应用中使用 SQLite 数据库来存储数据是相当常见的.在 UWP 平台中要使用 SQLite,一般会使用 SQLite for Universal Windows Platform 和 SQLit ...

  3. 在服务器中使用 Entity Framework 的 Migration 更新数据库

    在开发环境中,每次我们对要对数据库进行更改,比如增加修改表字段等.改好Entity类后,我们只需在Nuget程序包管理控制台运行 update-database 脚本却可: update-databa ...

  4. 使用 Entity Framework 7 进行 SQLite 的 CURD 操作

    原文地址:http://www.oschina.net/translate/sqlite-crud-operation-using-entity-framework 介绍 我善于使用传统的SQL查询风 ...

  5. Asp.net core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库

    Asp.net Core 3.1+EF Core2.2.6+Oracle.EntityFrameworkCore2.1.19连接Oracle数据库 1.前言 本次主要采用Asp.net core3.1 ...

  6. Entity Freamwork 6连接PostgreSql数据库

    原文 Entity Freamwork 6连接PostgreSql数据库 开发环境 VS 2015  Update 1   Postgre Sql 9.4 使用过程 1.使用Nuget在项目中添加对E ...

  7. 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)

    前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...

  8. EF框架组件详述【Entity Framework Architecture】(EF基础系列篇3)

    我们来看看EF的框架设计吧: The following figure shows the overall architecture of the Entity Framework. Let us n ...

  9. 1.翻译:EF基础系列--什么是Entity Framework?

    大家好,好久不见,EF系列之前落下了,还是打算重新整理一下. 先说说目前的打算:先简单了解一下EF基础系列-->然后就是EF 6 Code-First系列-->接着就是EF 6 DB-Fi ...

随机推荐

  1. HTML 转 PDF

    使用WkHtmlToXSharp,免费的软件,内集成了chrome 的内核,可以对CSS进行渲染,很好用 是需要传入 HTML 网页地址就可以转化为PDF文件,不过网页的编码要是 utf-8 Nuge ...

  2. .Net Core Linux centos7行—hyper-v安装linux系统和.net core sdk

    下载linux系统,选择安装centos7 下载地址:https://www.centos.org/download/ 安装centos7 hyper-v选择新建虚拟机 根据向导一路next,虚拟机代 ...

  3. idea 静态资源不能即时更新

  4. CSS3选择器:nth-of-type

    碰到了个选择器,:nth-of-type <!DOCTYPE html> <html> <head> <meta http-equiv="Conte ...

  5. ReactNative 从环境和第一个demo说起,填坑教程

    一.React-Native MacOS必备环境配置: 1.安装homebrew(这东西可以理解为命令行的app商店) /usr/bin/ruby -e "$(curl -fsSL http ...

  6. 优化php代码 - 字符串echo输出 逗号也可作php连接符

    2016年12月12日10:00:16 ====================== 网页访问速度的提升,是可以通过代码的优化来实现的.代码的优化,并不是说代码越少越好,而是主要看代码的运行能力和执行 ...

  7. 动手实验iptables的NAT功能实现流量穿透

    1.NAT和iptables理论见: http://lustlost.blog.51cto.com/2600869/943110 2.引子 近期,有同事抱怨说数据入库时,由于数据库所在的服务器只有内网 ...

  8. Long Short-Term Memory (LSTM)公式简介

    Long short-term memory: make that short-term memory last for a long time. Paper Reference: A Critica ...

  9. maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project web_nanchang

    maven打包成war时,报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default- ...

  10. @SuppressWarnings的参数

    @SuppressWarnings 是J2EE的最后一个批注,该批注的作用是告诉编译器对被批注的元素内部的某些警告保持静默 @SuppressWarnings("unchecked" ...