EntityFramework SQLite
安装完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的更多相关文章
- UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?
选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...
- EntityFramework Core技术线路(EF7已经更名为EF Core,并于2016年6月底发布)
官方文档英文地址:https://github.com/aspnet/EntityFramework/wiki/Roadmap 历经延期和更名,新版本的实体框架终于要和大家见面了,虽然还有点害羞.请大 ...
- EntityFrameWork连接多Db配置
如题所示,EF作为微软主推的ORM工具,最新版本已经是7,说明有很多人在使用它做项目.在使用过程中,可能会连接不同的数据库,本文介绍的是连接SqlServer,MySql和SQLite三种,并且可以互 ...
- SQLite 的 CodeFirst 模式
目录 问题描述 解决方案 安装依赖包 修改程序配置 App.config 创建模型对象 Person.cs 创建数据上下文 PersonDbContext.cs 主程序调用 Program.cs 注意 ...
- Microsoft.EntityFrameworkCore.Sqlite的学习
SQLite in ASP.NET Core with EntityFrameworkCore ASP.NET Core 2: Using SQLite as a light weight datab ...
- [转]EntityFramework Core技术线路(EF7已经更名为EF Core,并于2016年6月底发布)
本文转自:http://www.cnblogs.com/VolcanoCloud/p/5572408.html 官方文档英文地址:https://github.com/aspnet/EntityFra ...
- 使用 Entity Framework 7 进行 SQLite 的 CURD 操作
原文地址:http://www.oschina.net/translate/sqlite-crud-operation-using-entity-framework 介绍 我善于使用传统的SQL查询风 ...
- Sqlite && EF Code FIRST 终极解决方案 2019.5.17
Sqlite && EF Code FIRST 终极解决方案 2019.5.17 包括根据模型自动生成数据库,初始化数据,模型改变时的自动数据迁移等 2019.12.25 更新 支持E ...
- Entity Framework7 入门之全功能.NET版本下使用EF7(含源码)另附数据迁移常见错误处理
Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7 昨天,我们介绍了EF的新特性和开发计划,如果你还不了解,请移步 Entity Fr ...
随机推荐
- UVa1161 Objective: Berlin(最大流)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- ural 1251. Cemetery Manager
1251. Cemetery Manager Time limit: 1.0 secondMemory limit: 64 MB There is a tradition at the USU cha ...
- (转)yarn 集群部署,遇到的问题小结
link:http://blog.csdn.net/uniquechao/article/details/26449761 版本信息: hadoop 2.3.0 hive 0.11.0 1. ...
- yarn map failed
Container launch failed for container_1385017085286_4943_01_000053 : org.apache.hadoop.yarn.exceptio ...
- [R]R的工作流
最近处理数据时,一直在纠结程序的结构该如何构建,以减少很多简单又很耗时的工作. 刚好把Rob J Hyndman的blog给浏览了一遍,发现一篇2009年的文章,很有启发. 原文: Workflow ...
- IOS UI segmentedControl UISegmentedControl 常见属性和用法
UISegmentedControl中一些常见的属性和用法 //设置以图案作为分段的显示,仅需要图案的轮廓,这样颜色为分段的背景颜色 // NSArray *items = @[[UIImage ...
- 51Nod 1256 乘法逆元 Label:exgcd
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K ...
- PHP程序员 新人求职 平台
投简历网站:大街网:中下,但是反馈速度不错拉勾网:中上,工资好像很多都很不错实习僧:不太熟悉,中等吧猎聘网:好像不错,本人不常用海投网:不清楚,没用过以上评级都是个人感觉而已,不代表正式评价个人感觉以 ...
- 【BZOJ】1115: [POI2009]石子游戏Kam
http://www.lydsy.com/JudgeOnline/problem.php?id=1115 题意:n堆石子,个数是从左到右单增.每一次可以从任意堆取出任意石子,但要保持单增这个性质.问先 ...
- 【BZOJ1088】[SCOI2005]扫雷Mine 递推
调LCT奔溃,刷水调节一下. #include <iostream> #include <cstdio> #include <cstring> using name ...