记录一下通过 EntityFramework6 来操作sqlite过程

环境:

  • visual studio 2017
  • .net 4.5
  • Console Application(项目类型)
  • sqlite3
  • navicat for sqlite

设计数据库

我使用了 navicat for sqlite 这个图形界面工具来快速生成数据库的;

非常简单,打开 navicat ,按照下图操作即可



新建表:

Entry表(Id为主键,勾选自增),Type_Id为外键.



EntryType表(Id为主键,勾选自增)

完事之后点击左上角的保存!

在visual studio中建立控制台项目,安装必要的nuget包

打开nuget包管理工具,

在Browse选项卡中搜索 System.Data.SQLite

安装相应的nuget包,如图所示



之后在nuget包管理工具中查看已安装的nuget包

如下图:



然后在解决方案浏览器下可以看到App.config文件,



进去修改一下内容,在 provider 节点下加入下面的内容:

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

建立 Entity 实体类

Entry 类 :

Entry
namespace MagicMemory.Core.Entities
{
public class Entry
{
public int Id { get; set; }
public string Key { get; set; }
public string Content { get; set; }
public int? EntryTypeId { get; set; }
public virtual EntryType Type { get; set; }
}
}

这里值得注意的是,Entry实体中有一个外键属性:

public virtual EntryType Type { get; set; }

一定要用virtual来修饰,这里不清楚为啥,我也是偶然看见的,不用virual就没用,可能因为是Dbfirst的原因,之前在.net core结合efcore使用的时候并不需要加virtual也行.这里的外键属性,Ef 会自动从数据库里相应的表中给我们映射过来.但是这个外键所在表也必须在 DbContext中作为DbSet<>的泛型参数存在才可以.

EntryType 类:

EntryType
using System;

namespace MagicMemory.Core.Entities

{

public class EntryType

{

public int Id { get; set; }

public string Name { get; set; }

}

}

实体类就这两个,差不多可以说明问题了,建立实体类的时候,实体类的属性名一定要和表的字段名相匹配(必须一样才行),名称不一样的话,则需要在属性的上方加一个注解属性 Column("Name").也可以使用fluentApi来进行配置,我跟喜欢这种方式,在DbContext中重写OnModelCreating()方法,对 column 进行配置.

当实体类型的属性名需要和不同名称的的表中的列进行映射时,可以使用下面的方法.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.BlogId)
.HasColumnName("blog_id");
}

继承DbContext,建立数据库上下文 xxxContext 类

EntryContext
using System.Data.Entity;
using System.Data.SQLite;
using MagicMemory.Core.Entities;
using SQLite.CodeFirst; namespace MagicMemory.DbStudy

{

public class EntryContext:DbContext

{

public EntryContext():base(new SQLiteConnection("Data Source=MagicMemory.Db"),true)

{

}
    protected override void OnModelCreating(DbModelBuilder builder)
{
builder.Entity&lt;Entry&gt;().ToTable(&quot;Entry&quot;);
builder.Entity&lt;Entry&gt;()
.Property(e =&gt; e.EntryTypeId)
.HasColumnName(&quot;EntryTypeId&quot;); builder.Entity&lt;EntryType&gt;().ToTable(&quot;EntryType&quot;); builder.Entity&lt;EntryTag&gt;().ToTable(&quot;EntryTagTable&quot;);
builder.Entity&lt;EntryTag&gt;()
.Property(t =&gt; t.Name)
.HasColumnName(&quot;TagName&quot;); base.OnModelCreating(builder); Database.SetInitializer(new SqliteDropCreateDatabaseWhenModelChanges&lt;EntryContext&gt;(builder));
} public DbSet&lt;Entry&gt; Entry { get; set; }
public DbSet&lt;EntryType&gt; EntryType { get; set; }
public DbSet&lt;EntryTag&gt; EntryTag { get; set; }
}

}

这里比较重要的是重写了 OnModelCreating() 方法,这个方法里面通过 fluent api的方式定义了实体类的属性和具体的数据库的字段的映射的关系;同时,在默认的构造函数里面,调用基类的构造函数,因为使用sqlite这个数据库,所以将继承了DbConnection的实例: new SQLiteConnection("[连接字符串]") 传递给基类的构造函数,用来连接数据库.

  </div>

通过EntityFramework操作sqlite(DbFirst)的更多相关文章

  1. 使用entityframework操作sqlite数据库

    首先要安装好,所需要的类库,通过NuGet来处理 http://stackoverflow.com/questions/28507904/vs-2015-sqlite-data-provider 安装 ...

  2. EF操作sqlite数据库时的项目兼容性问题

    问题:vs2015打不开vs2010建的操作sqlite的实体数据模型edmx文件 原因: 当前电脑必须先安装:驱动库及sqlite的vs拓展 正常情况下安装驱动和拓展后,vs2015就应该可以正常打 ...

  3. 【UWP】利用EF Core操作SQLite

    在以往开发中,一定要在vs中安装SQLite for Universal App Platform以及一款wrapper,如SQLitePCL.现在有了EntitfyFramewrok Core,我们 ...

  4. Java操作Sqlite数据库-jdbc连接

    Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...

  5. C#操作SQLite数据库

    SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configurati ...

  6. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  7. 操作SQLite的dbhelper

    操作SQLite的dbhelper public class DbHelper { string connStr = @"Data Source=" + System.Enviro ...

  8. python操作sqlite数据库

    root@cacti:~/box# cat convert.py #!/usr/bin/env python import sqlite3,time,rrdtool,os def boxstatus( ...

  9. .NET中操作SQLite

    C#操作SQLite Database C#下SQLite操作驱动dll下载:System.Data.SQLite C#使用SQLite步骤: (1)新建一个project (2)添加SQLite操作 ...

随机推荐

  1. C++ Crypto++ RSA加密资料收集

    C++利用Crypto++,vs2005环境下的RSA应用 基于Crypto++/Cryptopp的rsa密钥生成,rsa加密.解密,rsa签名.验签 Keys and Formats 使用Crypt ...

  2. OneZero第三周——预完成功能点统计

    本周OneZero将完成“摇一摇”功能. 功能点统计如下: 1.点击主页面“摇一摇”按钮,进入摇一摇界面. 2.摇一摇界面布局(上,中,下). 3.摇动手机,在摇一摇界面中显示一条消费记录. 4.继续 ...

  3. 子数整数(P1151&NOIP水题测试(2017082301))

    题目链接:子数整数 水题,不解释,自己看代码: #include<bits/stdc++.h> using namespace std; int main(){ int k; scanf( ...

  4. 2016-2017-2 20155312 实验三敏捷开发与XP实践实验报告

    1.研究code菜单 Move Line/statement Down/Up:将某行.表达式向下.向上移动一行 suround with:用 try-catch,for,if等包裹语句 comment ...

  5. lower_case_table_names

    http://blog.csdn.net/jesseyoung/article/details/40617031 1 简介    在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数 ...

  6. python学习 day22 (3月29日)----(生成器推导式)

    新手上路请多担待 1 2 封装 3 私有化封装 #__author : 'liuyang' #date : 2019/3/29 0029 上午 9:35 # 不想让别人看 修改 我的属性 # 源码来说 ...

  7. MyBatis中log4j 和 参数 和 分页和别名 功能

    1.配置全局文件,注意各个配置标签的顺序 properties?, settings?, typeAliases?, typeHandlers?, objectFactory?,   objectWr ...

  8. 2019.01.19 bzoj5457: 城市(线段树合并)

    传送门 线段树合并菜题. 题意简述:给一棵树,每个节点有bib_ibi​个aia_iai​民族的人,问对于每棵子树,子树中哪个民族的人最多,有多少人. 思路: 直接上线段树合并,边合并边维护答案即可. ...

  9. 深度优先搜索DFS和广度优先搜索BFS

    DFS简介 深度优先搜索,一般会设置一个数组visited记录每个顶点的访问状态,初始状态图中所有顶点均未被访问,从某个未被访问过的顶点开始按照某个原则一直往深处访问,访问的过程中随时更新数组visi ...

  10. 比较jquery中的after(),append(),appendTo()方法

    html页面: <p id="myp1">我的兴趣爱好是:</p> <button id="b1">after函数</ ...