安装完sqlite的nuget包后,还要设置App.config文件才能正常使用

1.  在<providers>节点添加一条提供器配置

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

<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>

这项配置看起来好像没有用到,但是不添加的话又会发生错误.

2.  添加一个连接字符串节点<connectionStrings>用于添加数据库的连接字符串

如添加一条连接字符串<add name="TestDb" connectionString="Data Source=TestDb.db" providerName="System.Data.SQLite.EF6"/>

<connectionStrings>
<add name="TestDb" connectionString="Data Source=TestDb.db" providerName="System.Data.SQLite.EF6"/>
</connectionStrings>

name="TestDb" 指定这条连接字符串的名称为"TestDb", 用在自定义的数据库上下文类中

connectionString="Data Source=TestDb.db" 指定数据源为"TestDb.db", 即数据库文件名为"TestDb.db",默认保存路径为工作目录(可自己指定其它路径)

providerName="System.Data.SQLite.EF6" 指定数据提供器

3. 数据库上下文类

例:(注意其中的数据库连接字符串的使用方法)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using SQLite.CodeFirst;
using System.Data.Entity.ModelConfiguration.Conventions; namespace WindowsFormsApplication_sqlite2
{
class TestDbContext :DbContext
{
//base("TestDb")调用基类构造函数,可指定连接字符串名称
public TestDbContext() : base("TestDb") { } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//base.OnModelCreating(modelBuilder);
//关闭所有级联删除
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<TestDbContext>(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
} public DbSet<TestA> TestAs { get; set; }
public DbSet<TestB> TestBs { get; set; }
} class TestA
{
public long Id { get; set; }
public string Barcode { get; set; }
public string Name { get; set; }
} class TestB
{
public long Id { get; set; }
public long TestAId { get; set; }
public DateTime Date { get; set; }
[ForeignKey("TestAId")]
public TestA TestA { get; set; }
}
}

EntityFramework SQLite的更多相关文章

  1. UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?

    选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...

  2. EntityFramework Core技术线路(EF7已经更名为EF Core,并于2016年6月底发布)

    官方文档英文地址:https://github.com/aspnet/EntityFramework/wiki/Roadmap 历经延期和更名,新版本的实体框架终于要和大家见面了,虽然还有点害羞.请大 ...

  3. EntityFrameWork连接多Db配置

    如题所示,EF作为微软主推的ORM工具,最新版本已经是7,说明有很多人在使用它做项目.在使用过程中,可能会连接不同的数据库,本文介绍的是连接SqlServer,MySql和SQLite三种,并且可以互 ...

  4. SQLite 的 CodeFirst 模式

    目录 问题描述 解决方案 安装依赖包 修改程序配置 App.config 创建模型对象 Person.cs 创建数据上下文 PersonDbContext.cs 主程序调用 Program.cs 注意 ...

  5. Microsoft.EntityFrameworkCore.Sqlite的学习

    SQLite in ASP.NET Core with EntityFrameworkCore ASP.NET Core 2: Using SQLite as a light weight datab ...

  6. [转]EntityFramework Core技术线路(EF7已经更名为EF Core,并于2016年6月底发布)

    本文转自:http://www.cnblogs.com/VolcanoCloud/p/5572408.html 官方文档英文地址:https://github.com/aspnet/EntityFra ...

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

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

  8. Sqlite && EF Code FIRST 终极解决方案 2019.5.17

    Sqlite && EF Code FIRST 终极解决方案 2019.5.17 包括根据模型自动生成数据库,初始化数据,模型改变时的自动数据迁移等 2019.12.25 更新 支持E ...

  9. Entity Framework7 入门之全功能.NET版本下使用EF7(含源码)另附数据迁移常见错误处理

    Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7 昨天,我们介绍了EF的新特性和开发计划,如果你还不了解,请移步 Entity Fr ...

随机推荐

  1. aapt命令介绍及常用命令实践

    D:\>aapt -h ERROR: Unknown command '-h' Android Asset Packaging Tool Usage: aapt l[ist] [-v] [-a] ...

  2. DOM基础2

    插入元素 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  3. Poj1611The Suspects

    A - The Suspects Time Limit: 1000 MS Memory Limit: 20000 KB 64-bit integer IO format: %I64d , %I64u  ...

  4. 【BZOJ1864】[Zjoi2006]三色二叉树 树形DP

    1864: [Zjoi2006]三色二叉树 Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最 ...

  5. mysql的关于TABLE_SCHEMA的sql语句和nformation_schema表

    1.查询sjcenter数据库里开头为sj_demo和sj_onlyinv的所有表的总条数 select sum(table_rows) from (select table_name,table_r ...

  6. POJ3177 & 求边双联通分量

    题意: 给一张无向图,求加多少边使原图任意两点边双联通. SOL: 一个不会写边双点双强联通的傻逼. 一个结论:把一棵树变成满足条件的图需要加的边使入度为1的点数+1除以2.------>就是树 ...

  7. 常用JS正则表达式

    常用JS正则表达式 收集一些常用的JavaScript正则表达式匹配规则,比如匹配电话号码.Email.中文字符.身份证号.邮编.QQ号.过滤空白行.匹配特定数字等.觉得这玩意是很有用的,只不过自己水 ...

  8. osg中遇到的问题

    osg中遇到的问题 今天写程序的时候, 需要把键盘和鼠标消息转发出来, 就直接写了接口用signal丢出来了. 程序写的很多, 测试的时候却崩溃了.... 在场景中拖拽鼠标左键的时候, 会发现在扔出鼠 ...

  9. docker 报Error: docker-engine-selinux conflicts with docker-selinux-1.9.1-25.el7.centos.x86_64

    root@ecshop Deploy]# yum -y install docker-engine-selinux.noarchLoaded plugins: fastestmirrorhttp:// ...

  10. GO语言练习:channel 工程实例

    1.工程代码 2.编译及运行 1.工程目录结构 $ tree cgss cgss ├── cgss.go └── src ├── cg │   ├── centerclient.go │   ├── ...