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 ...
随机推荐
- EF框架step by step(7)—Code First DataAnnotations(1)
Data annotation特性是在.NET 3.5中引进的,给ASP.NET web应用中的类提供了一种添加验证的方式.Code First允许你使用代码来建立实体框架模型,同时允许用Data a ...
- SQL SERVER 和ACCESS的数据导入导出
//批量导入Access string filepath = Server.MapPath("student.mdb"); stri ...
- C#/.NET Little Wonders: Use Cast() and OfType() to Change Sequence Type(zz)
Once again, in this series of posts I look at the parts of the .NET Framework that may seem trivial, ...
- 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest F. Friends
F. Friends time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- BZOJ4110 : [Wf2015]Evolution in Parallel
首先每个串都必须是$S$的子序列,否则无解. 按长度从小到大依次考虑每个串,如果它两边都不能放,那么无解. 如果能放一边,那么放进去,把待定的全部放入另一边. 如果两边都能放,那么看看能否待定,如果不 ...
- 栈式自动编码器(Stacked AutoEncoder)
起源:自动编码器 单自动编码器,充其量也就是个强化补丁版PCA,只用一次好不过瘾. 于是Bengio等人在2007年的 Greedy Layer-Wise Training of Deep Netw ...
- BZOJ1103[POI2007]大都市meg 题解
题目大意: 有一棵树,最先每条边的权值是1,然后给出n+m-1个操作,操作有两种:1.询问一个点到根的路径上的权值和:2.将一条边的权值改为0. 思路: 用dfs序将树化为序列,在dfs序中我们会保存 ...
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
关于证明可以参考题解http://codeforces.com/blog/entry/12739 就是将概率从大到小排序然后,然后从大到小计算概率 #include <iostream> ...
- c++实现des算法
程序分三部分,des头文件,des类实现,main函数调用. //panda //2013-4-13 //des //des.h class DES { private: //public: //明文 ...
- [友盟微博分享]does not contain bitcode. You must rebuild it with
1.我的 Xcode 明明打开了 bitcode 了,为什么还会报错呢?挺奇怪的. 2.上网一查,才知道,友盟需要 bitcode,Xcode 不支持,只要关闭bitcode就可以 了. 3.其实我也 ...