SQLite 的 CodeFirst 模式
目前 EF 是 .NET 平台下相当成熟的 ORM 框架,但是其最新发布的 6.x 版本依然不支持 SQLite 的 CodeFirst 模式,好在有大神已经在 Nuget 上发布的相应的 Package 来解决这个问题。笔者通过做一个小实验来验证一下。
问题描述
SQLite 本身不支持 CodeFirst 模式,当我们的数据模型因业务变化而需要修改的话,那对应的数据库表也要进行更改。这个时候,如果我们手动修改数据表的话就不太方便,因此我们需要想办法让其支持 CodeFirst 模式。笔者通过使用 SQLite.CodeFirst 来尝试解决上述问题。
解决方案
安装依赖包
首先我们创建一个控制台程序,安装如下 Package:
- System.Data.SQLite
- SQLite.CodeFirst
修改程序配置 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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<!--新增结点-->
<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" />
<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" /></DbProviderFactories>
</system.data>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<!--新增结点-->
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<!--新增结点-->
<connectionStrings>
<add name="sampledb" connectionString="data source=.\sampledb.db" providerName="System.Data.SQLite" />
</connectionStrings>
</configuration>
创建模型对象 Person.cs
[Table("person")]
public class Person
{
[Column("id"), Key, Autoincrement]
public int Id { get; set; }
[Column("firstname")]
public string FirstName { get; set; }
[Column("lastname")]
public string LastName { get; set; }
}
创建数据上下文 PersonDbContext.cs
public class PersonDbContext : DbContext
{
public DbSet<Person> People { get; set; }
/// <summary>
/// 从配置文件读取链接字符串
/// </summary>
public PersonDbContext() :
base("name = sampledb")
{
ConfigurationFunc();
}
/// <summary>
/// 代码指定数据库连接
/// </summary>
/// <param name="existingConnection"></param>
/// <param name="contextOwnsConnection"></param>
public PersonDbContext(DbConnection existingConnection, bool contextOwnsConnection) :
base(existingConnection, contextOwnsConnection)
{
ConfigurationFunc();
}
private void ConfigurationFunc()
{
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var initializer = new SqliteDropCreateDatabaseWhenModelChanges<PersonDbContext>(modelBuilder);
Database.SetInitializer(initializer);
}
}
此时,当我们修改数据模型时,不需要执行任何 migration 操作就可以将 数据表映射到新的模型上。下面我们在主程序中调用一下:
主程序调用 Program.cs
class Program
{
static void Main(string[] args)
{
var ptah = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sampledb.db");
var connection = SQLiteProviderFactory.Instance.CreateConnection();
connection.ConnectionString = $"Data Source={ptah}";
//using (var context = new PersonDbContext())
using (var context = new PersonDbContext(connection, false))
{
#region 预热:针对数据表较多的情况下建议执行下述操作
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var mappingColection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
mappingColection.GenerateViews(new List<EdmSchemaError>());
#endregion
#region 插入数据
Console.WriteLine("插入数据:");
context.People.Add(new Person { FirstName = "hippie", LastName = "zhou" });
context.SaveChanges();
#endregion
#region 查找数据
Console.WriteLine("查找数据:");
foreach (var people in context.People)
{
Console.WriteLine($"{people.Id} - {people.FirstName} - {people.LastName}");
}
#endregion
#region 更改数据
Console.WriteLine("更改数据:");
Person person = context.People.Where(p => p.Id == 1)?.FirstOrDefault();
person.LastName = "Puth";
context.SaveChanges();
#endregion
#region 删除数据
Console.WriteLine("删除数据:");
Person person = context.People.Where(p => p.Id == 1)?.FirstOrDefault();
context.People.Remove(person);
context.SaveChanges();
#endregion
Console.ReadKey();
}
}
}
注意事项
由于 SQLite.CodeFirst 的 Package 需要依赖 EntityFramework,以及为了能保证数据操作支持相关 LINQ 操作,在上述两个 Pacakge 安装的过程中,会同时安装其它依赖包,所以最终的项目依赖包如下所示:
- EntityFramework
- SQLite.CodeFirst
- System.Data.SQLite
- System.Data.SQLite.Core
- System.Data.SQLite.EF6
- System.Data.SQLite.Linq
此外,需要注意一下 System.Data.SQLite 与 System.Data.SQLite.Core 的区别:
System.Data.SQLite:The official SQLite database engine for both x86 and x64 along with the ADO.NET provider. This package includes support for LINQ and Entity Framework 6.
System.Data.SQLite.Core:The official SQLite database engine for both x86 and x64 along with the ADO.NET provider.
所以,为了能让 SQLite 支持 CodeFirst ,还是一件挺曲折的事情啊。
相关参考
SQLite 的 CodeFirst 模式的更多相关文章
- 【原创】EntityFramework Core 中使用 CodeFirst 模式时 PowerShell 版本问题及解决
一.描述: 在使用 Entity Framework Core 时,使用 CodeFirst 模式, 在 VS 中的 PMC(nuget 包管理 控制台) 控制台界面使用如下命令: Install-P ...
- 第十三节: EF的三种模式(三) 之 来自数据库的CodeFirst模式
一. 简介 [来自数据库的Code First模式]实质上并不是CodeFirst模式,而是DBFirst模式的轻量级版本,在该模式中取消了edmx模型和T4模板,直接生成了EF上下文和相应的类,该模 ...
- 第十四节: EF的三种模式(四) 之 原生正宗的 CodeFirst模式的默认约定
一. 简介 1. 正宗的CodeFirst模式是不含有edmx模型,需要手动创建实体.创建EF上下文,然后生成通过代码来自动映射生成数据库. 2. 旨在:忘记SQL.忘记数据库. 3. 三类配置:On ...
- 第十七节: EF的CodeFirst模式的四种初始化策略和通过Migration进行数据的迁移
一. 四种初始化策略 EF的CodeFirst模式下数据库的初始化有四种策略: 1. CreateDatabaseIfNotExists:EF的默认策略,数据库不存在,生成数据库:一旦model发生变 ...
- EF的CodeFirst模式自动迁移(适用于开发环境)
EF的CodeFirst模式自动迁移(适用于开发环境) 1.开启EF数据迁移功能 NuGet包管理器------>程序包管理控制台---------->Enable-Migrations ...
- GRDB使用SQLite的WAL模式
GRDB使用SQLite的WAL模式 WAL全称是Write Ahead Logging,它是SQLite中实现原子事务的一种机制.该模式是从SQLite 3.7.0版本引入的.再此之前,SQLi ...
- sqlite之WAL模式
链接 概述 在3.7.0以后,WAL(Write-Ahead Log)模式可以使用,是另一种实现事务原子性的方法. WAL的优点 在大多数情况下更快 并行性更高.因为读操作和写操作可以并行. 文件IO ...
- asp.net mvc CodeFirst模式数据库迁移步骤
利用Code First模式构建好基本的类后,项目也开始搭建完毕并成功运行,而且已经将数据库表结构自动生成了. 但是,我有新的类要加入,有字段需要修改,那怎么办呢,删库,跑路 ? 哈哈 利用数据库迁 ...
- 第十五节: EF的CodeFirst模式通过DataAnnotations修改默认协定
一. 简介 1. DataAnnotations说明:EF提供以特性的方式添加到 domain classes上,其中包括两类: A:System.ComponentModel.DataAnnota ...
随机推荐
- hive 命令行传入参数
azkban实现任务重跑 我们执行sql的方式是将hql文件上传到服务器本地.然后执行shell命令 hive " -f ./test_scheduler.hql 注:hive -e 是执行 ...
- 在Ubuntu登陆界面输入密码之后,黑屏一闪后,又跳转到登录界面
现象:在Ubuntu登陆界面输入密码之后,黑屏一闪后,又跳转到登录界面.原因:主目录下的.Xauthority文件拥有者变成了root,从而以用户登陆的时候无法都取.Xauthority文件.说明:X ...
- jzoj5929. 【NOIP2018模拟10.26】情书
动态规划: #include<bits/stdc++.h> using namespace std; int n,iv[30]; #define mo 998244353 typedef ...
- typescript handbook 学习笔记4
概述 这是我学习typescript的笔记.写这个笔记的原因主要有2个,一个是熟悉相关的写法:另一个是理清其中一些晦涩的东西.供以后开发时参考,相信对其他人也有用. 学习typescript建议直接看 ...
- php cli模式和浏览器访问下加载php.ini文件的注意事项[架构篇]
使用wampserver或Xampp时,会将配置文件放在一个统一的目录中去调用,这时如果都使用浏览器访问,自然是没有问题的,但是如果换成cli命令行模式运行,则会出现加载了的扩展无法使用的问题. 案例 ...
- Mac下命令行批量重命名
日常中碰到需要批量修改文件名怎么办?嗯,来终端先 案例:将Users/case目录下所有html文件修改为php文件 步骤: 1.进入目标文件夹 $ cd Users/case 2.执行以下命令 $ ...
- iOS之Settings.Bundle的应用
Settings.Bundle Settings.Bundle支持六种配置项分别是:Title,MultiValue,Group,Slider,ToggleSwitch,TextField . Tit ...
- 常用的 git 命令清单
(来自阮一峰的网络日志,看别人对git命令掌握的如此熟练,羡慕,但每次又记不得,无奈.供自己学习) git工作区,暂存区,版本库之间的关系: 我们建立的项目文件夹就是工作区,在初始化git(git i ...
- Intellij-忽略其他编译错误,运行当前文件
在使用Intellij IDEA的时候,有时候想在项目中写个main()方法或者单元测试测试个功能,但是有其他文件编译出错IDEA就提示我说不能运行,很是烦恼. 能不能像Eclipse一样呢,其他编译 ...
- dart之旅(三)- list
list, 在 js 中被称为数组, 但是和 js 中的数组还是有不少不同的地方,我们来看一个例子: // 声明一个长度不可变的 list List<int> fixedLengthLis ...