C# EF Core迁移数据库

现象:

在CodeFirst时,先写字段与表,创建数据库后,再添加内容

但字段与表会变更,比如改名·删除·增加等

需求:

当表字段变更时,同时变更数据库,执行数据库迁移


核心命令

Add-Migration firstMigration

update-database

步骤

打开程序包管理窗口

工具=>NuGet包管理器=>程序包管理控制台

Add-Migration代表迁移命令:后边参数为名称

输入命令:

cs 复制代码
Add-Migration firstMigration

但是可能会报错,提示EntityFramework的程序包未安装

Your target project 'ShangShangQian' doesn't reference EntityFramework. This package is required for the Entity Framework Core Tools to work. Ensure your target project is correct, install the package, and try again.

NuGet安装下

安装完毕,再执行Add-Migration firstMigration

No migrations configuration type was found in the assembly 'ShangShangQian'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

在程序集"ShangShangQian"中找不到迁移配置类型。(在Visual Studio中,可以使用Package Manager控制台中的"启用迁移"命令添加迁移配置)。

再去执行:启用迁移"命令

cs 复制代码
Enable-Migrations

哦......可能得加个数据库上下文,就是你继承DbContext的类

再执行下Enable-Migrations -ContextTypeName 加上命名空间与类名

cs 复制代码
Enable-Migrations -ContextTypeName ShangShangQian.EFCore.DataBaseContext

Checking if the context targets an existing database...

System.ArgumentException: The type 'DataBaseContext' does not inherit from DbContext. The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext

The type 'DataBaseContext' does not inherit from DbContext. The DbMigrationsConfiguration.ContextType property must be set to a type that inherits from DbContext.

正在检查上下文是否以现有数据库为目标。。。

系统ArgumentException:类型"DataBaseContext"不是从DbContext继承的。DbMigrationsConfiguration。ContextType属性必须设置为从DbContext继承的类型。

继承了啊,奇了怪了............

Goole下=>缺个 Microsoft.EntityFrameworkCore.Tools 装下

再执行添加启用迁移命令:

Enable-Migrations -ContextTypeName ShangShangQian.EFCore.DataBaseContext

Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Enable-Migrations' for Entity Framework 6.

Enable-Migrations is obsolete. Use Add-Migration to start using Migrations.

实体框架核心和实体框架6都已安装。实体框架核心工具正在运行。对实体框架6使用"EntityFramework6\启用迁移"。

"启用迁移"已过时。使用"添加迁移"开始使用迁移。

=======》去你姥姥的,坑我,这不回到了起点《=======

Add-Migration firstMigration

死马当活马医吧,执行试试

再跑下更新命令?

cs 复制代码
update-database

表模型变更,执行这俩行,去测试下 把表和字段增加·删除·试试,哦 报错了......

Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

CREATE TABLE "Chats" (

"ChatID" INTEGER NOT NULL CONSTRAINT "PK_Chats" PRIMARY KEY AUTOINCREMENT,

"FromID" INTEGER NOT NULL,

"ToID" INTEGER NOT NULL,

"Message" TEXT NOT NULL,

"Type" INTEGER NOT NULL,

"IsRead" INTEGER NOT NULL,

"SendTime" TEXT NOT NULL,

"ReadTime" TEXT NOT NULL,

"IsPushFrom" INTEGER NOT NULL,

"IsPushTo" INTEGER NOT NULL

);

SQLite Error 1: 'table "Chats" already exists'.

为啥执行更新数据库要创建表呢,db已经存在了啊

又坑了我半天......就这样半个世纪过去了

不要灰心,猛补各种教学,原来是流程问题

问题出在EnsureCreatedAsync这个函数上

首次执行Add-Migration firstMigration命令,生成的迁移文件全是创建指令

再执行update-database

会执行创建表的命令,

但是之前通过EnsureCreatedAsync函数创建db数据库,已有表

所以报错,把db 与函数删除就可以了

创建数据库应由数据库迁移命令完成 update database

再此执行就可以

更高级的数据库功能请自行研究吧,如回退等

你也去补习下吧


文章写的有点乱

原本更新数据库分分钟的事,鼓捣半天,主要还是不熟

不过管他呢,代码能跑就行

相关推荐
小乌龟不会飞5 分钟前
Ubuntu 安装 etcd 与 etcd-cpp-apiv3
数据库·etcd
计算机毕设定制辅导-无忧学长8 小时前
西门子 PLC 与 Modbus 集成:S7-1500 RTU/TCP 配置指南(一)
服务器·数据库·tcp/ip
程序员柳8 小时前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui
梦在深巷、9 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
IT乌鸦坐飞机9 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
IT_10249 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
祁思妙想10 小时前
八股学习(三)---MySQL
数据库·学习·mysql
惊骇世俗王某人10 小时前
1.MySQL之如何定位慢查询
数据库·mysql
秦歌66611 小时前
向量数据库-Milvus快速入门
数据库·milvus
Edingbrugh.南空12 小时前
Flink SQLServer CDC 环境配置与验证
数据库·sqlserver·flink