博客
关于我
.Net Core 3.1 EF Core Migration使用CLI数据迁移和同步
阅读量:427 次
发布时间:2019-03-06

本文共 2734 字,大约阅读时间需要 9 分钟。

一、前言

    数据迁移可使用“包管理器控制台”(PMC) 或 CLI。本文列出使用CLI数据迁移命令和一些问题及解决方案。

二、迁移命令

    以下列出添加迁移文件、撤销迁移、更新到数据库、删除数据库基本方法。如需指定DbContext,则在语句后加  -c DbContext名  。如需查看帮助可在命令后加  -h 。

    1、创建迁移文件,迁移文件名必填

dotnet ef migrations add 迁移文件名

    2、撤销迁移,只能在未更新数据库前撤销

dotnet ef migrations remove

    3、更新到数据库

dotnet ef database update

     4、删除数据库

    注意不是删除数据更改,是删除数据库,慎用。

dotnet ef database drop

 

三、问题与解决

    1、问题

无法执行,因为找不到指定的命令或文件。

可能的原因包括:
*你拼错了内置的 dotnet 命令。
*你打算执行 .NET Core 程序,但 dotnet-install 不存在。
*你打算运行全局工具,但在路径上找不到名称前缀为 dotnet 的可执行文件。

 解决:打开CMD,输入:

dotnet tool install -g dotnet-ef

    2、问题

The EF Core tools version '3.1.0' is older than that of the runtime '3.1.2'. Update the tools for the latest features and bug fixes.

解决:需要更新EF Core tools版本,CMD中执行:

dotnet tool update -g dotnet-ef

    3、问题

It was not possible to find any compatible framework version

The framework 'Microsoft.NETCore.App', version '3.1.2' was not found.
- The following frameworks were found:
2.1.9 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
2.1.15 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
3.1.1 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:

- https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=3.1.2&arch=x64&rid=win10-x64

解决:到最后提供的网址下载SDK安装

    4、问题

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

解决:需要在网站目录执行

    5、问题

More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

解决:如果有多个DBContext,需要指定迁移哪个DBContext。如:

dotnet ef migrations add InitialCreate -c DBContext名称

    6、问题

Your target project 'Do.TmsApi' doesn't match your migrations assembly 'Do.Models'. Either change your target project or change your migrations assembly.

Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Do.TmsApi")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

解决:如果DBContext和启动程序不在一个程序集,需要指定要迁移的程序集。代码中添加要迁移的程序集名称:

options.UseSqlServer(connection, b => b.MigrationsAssembly("Do.TmsApi"))

 

转载地址:http://drsuz.baihongyu.com/

你可能感兴趣的文章
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
查看>>
mysql中的字段如何选择合适的数据类型呢?
查看>>
MySQL中的字符集陷阱:为何避免使用UTF-8
查看>>
mysql中的数据导入与导出
查看>>
MySQL中的时间函数
查看>>
mysql中的约束
查看>>
MySQL中的表是什么?
查看>>
mysql中穿件函数时候delimiter的用法
查看>>
Mysql中索引的分类、增删改查与存储引擎对应关系
查看>>
Mysql中索引的最左前缀原则图文剖析(全)
查看>>
MySql中给视图添加注释怎么添加_默认不支持_可以这样取巧---MySql工作笔记002
查看>>
Mysql中获取所有表名以及表名带时间字符串使用BetweenAnd筛选区间范围
查看>>
Mysql中视图的使用以及常见运算符的使用示例和优先级
查看>>
Mysql中触发器的使用示例
查看>>
Mysql中设置只允许指定ip能连接访问(可视化工具的方式)
查看>>