C# EF Core迁徙数据库

打印 上一主题 下一主题

主题 563|帖子 563|积分 1689

征象:

在CodeFirst时,先写字段与表,创建数据库后,再添加内容
但字段与表会变更,比如改名·删除·增长等
需求:

当表字段变更时,同时变更数据库,实验数据库迁徙

核心命令

Add-Migration firstMigration 
update-database
步调

打开步伐包管理窗口
工具=>NuGet包管理器=>步伐包管理控制台


Add-Migration代表迁徙命令:后边参数为名称
输入命令:
  1. 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控制台中的“启用迁徙”命令添加迁徙配置)。

再去实验:启用迁徙”命令
  1. Enable-Migrations
复制代码

哦……大概得加个数据库上下文,就是你继承DbContext的类

再实验下Enable-Migrations -ContextTypeName 加上命名空间与类名
  1. 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
死马当活马医吧,实验试试

再跑下更新命令?
  1. update-database
复制代码

表模型变更,实验这俩行,去测试下 把表和字段增长·删除·试试,哦 报错了……
Failed executing DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "Chats" (
    "ChatID" INTEGER NOT NULL CONSTRAINT "K_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
再此实验就可以

更高级的数据库功能请自行研究吧,如回退等
你也去补习下吧


文章写的有点乱
本来更新数据库分分钟的事,鼓捣半天,主要还是不熟 
不过管他呢,代码能跑就行

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表