asp.net core webAPI学习笔记(六)数据库迁移

IDE 版本:Visual Studio 2017 15.9.7
asp.net core 版本 2.1.1

本文档说明需在
asp.net core webAPI学习笔记(一)项目搭建
的基础下进行

开发环境下的数据库迁移

在上一篇笔记中我们实现了根据现有数据库创建实体类的自动化操作,那如果数据库丢失了,我们该如何快速的根据现有实体类代码创建数据库呢

1. 新建数据库

当前情况是针对第一次对数据库进行迁移
安装NuGet包 Microsoft.EntityFrameworkCore.Tools
在这里插入图片描述
这里要选择与asp.net core版本相同
在这里插入图片描述

更改数据库名,这里的数据库会在接下来的命令行执行中自动创建
在这里插入图片描述
打开程序包管理控制台
在这里插入图片描述
输入命令 添加数据库迁移类

Add-Migration FirstMigration

会在项目中添加文件夹
在这里插入图片描述
输入数据库迁移命令

Update-Database -Verbose

创建完成
在这里插入图片描述

2. 更新数据库

当前情况是对数据库进行增量迁移,也就是实体类添加了新的字段,需要同步更新数据库表
[TODO]

生产环境下的数据库迁移

在生产环境部署时,EFcore同样可以使用命令行来进行数据库创建操作

1. 新建数据库

对项目进行发布
在这里插入图片描述
选择文件系统发布
在这里插入图片描述
发布后的文件目录结构
在这里插入图片描述
当前目录新建deploy-ef2dot1-migrations.bat文件,文件名可以自定义,写入以下代码

@ECHO OFF

REM *************************************************
REM here are the args for this script
REM 
REM deploy-ef2dot1-migrations.bat migrationsDllName [startupDllName] [dbContextClassName] 
REM 
REM Any of the args in [brackets] are optional
REM 
REM *************************************************

if [%1] EQU [] (
    ECHO no parameters specified
    ECHO this isn't going to work
    ECHO.
    ECHO here are the args for this script
    ECHO.
    ECHO deploy-ef2-migrations.bat migrationsDllName [startupDllName] [dbContextClassName] 
    ECHO.
    ECHO Any of the args in [brackets] are optional

    EXIT /B
)

set EfMigrationsNamespace=%1

if [%2] EQU [] (
    ECHO no additional parameters specified
    ECHO assuming everything in %1

    set EfMigrationsDllDepsJson=%~n1.deps.json
    set EfMigrationsDllRuntimeConfig=%~n1.runtimeconfig.json
    set EfMigrationsDllName=%1
    set StartupDllName=%1
) else (
    ECHO additional parameters were specified
    ECHO assuming migrations in one DLL and startup DLL is %2
    
    set EfMigrationsDllDepsJson=%~n2.deps.json
    set EfMigrationsDllRuntimeConfig=%~n2.runtimeconfig.json
    set EfMigrationsDllName=%1
    set StartupDllName=%2
)

set DllDir=%cd%
set PathToNuGetPackages=%USERPROFILE%\.nuget\packages
set PathToNuGetPackages_Fallback1="C:\Program Files (x86)\Microsoft SDKs\NuGetPackagesFallback"
set PathToNuGetPackages_Fallback2="C:\Program Files\dotnet\sdk\NuGetFallbackFolder"

set PathToEfDll="not-found"

set PathToEfDll_Option=%cd%\ef.dll

if %PathToEfDll% EQU "not-found" (
  if exist %PathToEfDll_Option% (
      ECHO found ef.dll at %PathToEfDll_Option%
      set PathToEfDll=%PathToEfDll_Option%
  ) else (
    ECHO ef.dll not found at %PathToEfDll_Option%      
  )
)

set PathToEfDll_Option=%programfiles%\dotnet\sdk\2.1.301\DotnetTools\dotnet-ef\2.1.1\tools\netcoreapp2.1\any\tools\netcoreapp2.0\any\ef.dll

if %PathToEfDll% EQU "not-found" (
  if exist %PathToEfDll_Option% (
      ECHO found ef.dll at %PathToEfDll_Option%
      set PathToEfDll="%PathToEfDll_Option%"      
  ) else (
    ECHO ef.dll not found at %PathToEfDll_Option%      
  )
)

set PathToEfDll_Option=%PathToNuGetPackages_Fallback1%\microsoft.entityframeworkcore.tools.dotnet\2.0.1\tools\netcoreapp2.0\ef.dll

if %PathToEfDll% EQU "not-found" (
  if exist %PathToEfDll_Option% (
      ECHO found ef.dll at %PathToEfDll_Option%
      set PathToEfDll=%PathToEfDll_Option%
  ) else (
    ECHO ef.dll not found at %PathToEfDll_Option%      
  )
)

set PathToEfDll_Option=%PathToNuGetPackages%\microsoft.entityframeworkcore.tools.dotnet\2.0.0\tools\netcoreapp2.0\ef.dll

if %PathToEfDll% EQU "not-found" (
  if exist %PathToEfDll_Option% (
      ECHO found ef.dll at %PathToEfDll_Option%
      set PathToEfDll=%PathToEfDll_Option%
  ) else (
    ECHO ef.dll not found at %PathToEfDll_Option%      
  )
)

if %PathToEfDll% EQU "not-found" (
  ECHO.
  ECHO **** ERROR: could not find ef.dll ****
  ECHO.
  ECHO OH! SUCH UNRELENTING FAILURE! 
  ECHO But it wasn't like we didn't try.  This script actually looked in different 3 places for ef.dll.
  ECHO Let's face it.  It's that ef.dll that we really need to do an ef migration deploy with just the DLLs.
  ECHO But that ef.dll is tricky to find.
  ECHO.
  ECHO Be sure to tell the EF Core team how hard it is to deploy migrations via DLL.
  ECHO And let them know that it's mostly because it's impossible to find the ef.dll.
  ECHO It would be so much easier if we could just grab ef.dll during the build and 
  ECHO have it show up as a dependency in our "dotnet publish" output dir.
  ECHO.
  ECHO Oh? You want the link to their github project so you can mention it directly 
  ECHO to them in a discussion and tag @benday in that discussion? 
  ECHO That's a great idea!  Here ya go.  Here's the link to the EF Core project.
  ECHO https://github.com/aspnet/EntityFrameworkCore
  ECHO Just remember to be polite.  And also remember that @bricelam is a good guy.  
  ECHO.
  ECHO And on that note, exiting...
  ECHO.
  ECHO ps. sorry your ef core migrations didn't deploy.  :(
  EXIT /b 1
)

if [%3] EQU [] (
    ECHO no dbcontext name parameter specified
    ECHO here's hoping that everything works out for you
    ECHO. 
    ECHO here goes nuthin'...
    ECHO.

    dotnet exec --depsfile .\%EfMigrationsDllDepsJson% --additionalprobingpath %PathToNuGetPackages% --additionalprobingpath %PathToNuGetPackages_Fallback1% --additionalprobingpath %PathToNuGetPackages_Fallback2% --runtimeconfig %EfMigrationsDllRuntimeConfig% %PathToEfDll% database update --assembly .\%EfMigrationsDllName% --startup-assembly .\%EfMigrationsDllName% --project-dir . --verbose --root-namespace %EfMigrationsNamespace%    
) else (
    ECHO dbcontext name parameter was specified
    ECHO the dbcontext class name I'll be using is %3
    ECHO. 
    ECHO here goes nuthin'...
    ECHO.
    
    dotnet exec --depsfile .\%EfMigrationsDllDepsJson% --additionalprobingpath %PathToNuGetPackages% --additionalprobingpath %PathToNuGetPackages_Fallback1% --additionalprobingpath %PathToNuGetPackages_Fallback2% --runtimeconfig %EfMigrationsDllRuntimeConfig% %PathToEfDll% database update --assembly .\%EfMigrationsDllName% --startup-assembly .\%EfMigrationsDllName% --project-dir . --verbose --root-namespace %EfMigrationsNamespace% --context %3
)

找到ef.dll, 2.1版本的位置是在
C:\Users\Amon.nuget\packages\microsoft.entityframeworkcore.tools\2.1.1\tools\netcoreapp2.0\any\ef.dll

放入当前目录中
在这里插入图片描述
命令行跳转到当前目录,执行命令

deploy-ef2dot1-migrations.bat [项目dll名称] [实体类所在的dll名称] [DbContext类名称]

对于我当前的项目来说,应该输入

deploy-ef2dot1-migrations.bat testProduce testProduce testProduceContext

由于实体类放在了主项目中,所以第一个参数和第二个参数都是主项目的namespace

在这里插入图片描述
执行成功
以上内容参考自这里

2. 更新数据库

[TODO]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值