创建项目,应用EF访问SQLite

1、创建项目

项目结构初始结构如下图所示,Netage.Data.SQLite 类库项目用于定义访问数据的接口和方法,Netage.SQLiteTest.UI 控制台项目引用 Netage.Data.SQLite 类库,调用其相应的方法来访问数据。

2、在项目中加入SQLite类库

右键 Netage.Data.SQLite 项目,选择"Manage Nuget Packages"菜单,在输入框中输入"System.Data.SQLite",查询到"System.Data.SQLite(x86/x64)",并单击安装。如图所示:

安装完成后,在"Netage.Data.SQLite"项目中就引入了相应的类库。

3、定义数据上下文及相应的实体类

public class MyContext : DbContext
{
public DbSet<Person> Persons { get; set; } public MyContext()
: base("SqliteTest")
{ }
}
public class Person
{
public int Id { get; set; } public string Name { get; set; } public DateTime BirthDay { get; set; }
}

4、修改配置文件

在安装 "System.Date.SQLite(x86/x64)" 时,会默认在类库项目的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>
<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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<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>
</configuration>

这些信息都是在安装SQLite时自动配置的,由于我们在MyContext定义数据连接字符串名称为"SqliteTest",所以需要中配置文件中加入连接字符串信息。

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

5、在控制台项目中通过MyContext访问数据

(1) 引用 "Netage.Data.SQLite"类库项目

(2) 引入其他DLL

(3) 通过MyContext访问数据

class Program
{
static void Main(string[] args)
{
using (var context = new MyContext())
{
Console.WriteLine(context.Persons.Count());
} Console.WriteLine("运行结束");
Console.ReadKey();
}
}

(4)运行控制台项目

(5)手动把安装包中的"packages\System.Data.SQLite.Core.1.0.103\build\net45" 中的x64及x86复制到控制台项目的Bin\Debug目录中,如下所示。

(6) 再次运行控制台项目

(7)再次修改配置文件,修改的配置已经被红色部分标识,如下所示:

<?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>
<system.data>
<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>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<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>
<connectionStrings>
<add name="SqliteTest" connectionString="data source=SqliteTest.db" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
</configuration>

(8) 再次运行应用程序,如下所示:

这是因为EF SQLite不支持CodeFirst模式,因为到目前为止,我们还没有创建数据库及表结构,所以才有此错误。

通过SQLite Expert创建数据库及表结构

1、创建数据库,指定数据库文件名及文件存放位置

2、创建表结构,添加列,并设置列的属性

3、测试表创建完成,可以再自己尝试去操作这个工具的其他功能,这里不再细说。

应用EF访问SQLite数据库数据

因为上面已经将数据库文件存放到了别的位置 ,可以需要修改配置文件以特定新创建的数据库。

 <add name="SqliteTest" connectionString="data source=C:\Users\paulhuang\Desktop\SQLiteTest" providerName="System.Data.SQLite.EF6" />

这时我们就可以书写LINQ代码来测试相应的功能了。

1、插入数据

2、修改数据

关于通过EF访问SQLite的基本操作基本上到这里了,如果有不正确的地方欢迎指正。

应用EF访问SQLite数据的更多相关文章

  1. 【C#】使用EF访问Sqlite数据库

    原文:[C#]使用EF访问Sqlite数据库 1. 先上Nuget下载对应的包 如图,搜索System.Data.SQLite下载安装即可,下载完之后带上依赖一共有这么几个: EntityFramew ...

  2. [转]html5 js 访问 sqlite 数据库的操作类

    本文转自:http://blog.csdn.net/tsxw24/article/details/7613815 webkit 核心的浏览器提供了 3个 api接口,用于访问本地sqlite数据,但使 ...

  3. ASP.NET Core 使用 EF 框架查询数据 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 使用 EF 框架查询数据 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 使用 EF 框架查询数据 上一章节我们学习了如何设置 ...

  4. 【asp.net core 系列】8 实战之 利用 EF Core 完成数据操作层的实现

    0. 前言 通过前两篇,我们创建了一个项目,并规定了一个基本的数据层访问接口.这一篇,我们将以EF Core为例演示一下数据层访问接口如何实现,以及实现中需要注意的地方. 1. 添加EF Core 先 ...

  5. 深入理解 EF Core:EF Core 读取数据时发生了什么?

    阅读本文大概需要 11 分钟. 原文:https://bit.ly/2UMiDLb 作者:Jon P Smith 翻译:王亮 声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的.其中可能 ...

  6. 深入理解 EF Core:EF Core 写入数据时发生了什么?

    阅读本文大概需要 14 分钟. 原文:https://bit.ly/2C67m1C 作者:Jon P Smith 翻译:王亮 声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的.其中可能 ...

  7. 【Android】13.2 使用自定义的CursorAdapter访问SQLite数据库

    分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 SQliteDemo1的例子演示了SimpleCursorAdapter的用法,本节我们将使用用途更广的自定义的游 ...

  8. 【Android】13.1 用Android自带的API访问SQLite数据库

    分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 这一节我们先来看看如何直接用Android自带的API创建和访问SQLite数据库. 1.创建SQLite数据库 ...

  9. 【Android】13.0 第13章 创建和访问SQLite数据库—本章示例主界面

    分类:C#.Android.VS2015: 创建日期:2016-02-26 一.简介 Android 内置了三种数据存取方式:SQLite数据库.文件.SharedPreferences. 这一章我们 ...

随机推荐

  1. 【转】nginx中的ngx_cdecl

    http://blog.csdn.net/midion9/article/details/50605337 看nginx的代码时,发现有些函数返回值之后,还有一个ngx_cdecl关键字,如:   1 ...

  2. Access 数据库连接 字符串

    <!--Microsoft.Practices.EnterpriseLibrary.Data.dll 操作引用程序集--> <connectionStrings> <ad ...

  3. html5之history对象 控制浏览器前进或后退事件

    一.摘要: 总结用history对象操作浏览器的历史记录的方法,在项目中使用的是mui框架,总结中包括我在实际项目中遇到的问题. 二.总结: 实现效果: 实现代码: 上面的编辑页面加载的时候就要先调用 ...

  4. lumen 构建api(dingo api)

    什么是 API API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力, ...

  5. 使用图灵机器人API实现聊天机器人

    使用图灵机器人的API需要先注册,获取key才行,这我就不说了,自己到http://www.tuling123.com/注册一个账号即可. 下面就是一个简单的python调用API实现聊天机器人的简易 ...

  6. Python字符串处理

    字符串输入: my_string = raw_input("please input a word:") 字符串判断: (1) 判断是不是纯字母 my_string.isalpha ...

  7. Linux命令(23)grep命令的使用

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

  8. 如何使用Math对象快速计算数组中的最大值或最小值

    Math 对象下包含 min() 和 max() 方法 用于确定一组数值中的最大值和最小值.这两个方法都可以接收任意多个数值参数. var max = Math.max(1,2,3,4,5,6); c ...

  9. emacs配置eslint 语法检查.找不到node解决

    使用emacs配置eslint 当调用语法检查时报错 Suspicious state from syntax checker javascript-eslint: Checker javascrip ...

  10. Nhibernate对应关系参数介绍

    一.多对一关联映配置介绍 <many-to-one name="PropertyName" 属性名 column="column_name" 数据库字段名 ...