ef数据迁移命令总结之Update-Database

孙渝
2023-12-01
ef数据迁移命令总结之Update-Database

首先我们可以在vs的程序包管理控制台输入 get-help Add-Migration -detailed以查看详细信息。

使用数据迁移命令,并且了解详细的参数信息,从而可以在拥有多个迁移文件,多个项目,多个DBcontext的解决方案中去指定生成相应的数据库。而且它还拥有类似代码管理工具的一些历史记录回滚功能,如果使用entity framework,那么这是一个值得花一些心思去详细了解的命令。

摘要
Applies any pending migrations to the database.
将任何挂起的迁移应用到数据库。
完整的示例语法如下:

Update-Database -Force -ProjectName 1-3EntityFrameWorkDemo  -StartUpProjectName 1-3EntityFrameWorkDemo -ConfigurationTypeName EntityFrameWorkDemo.Migrations.Configuration -ConnectionStringName EmployeeDBContext -Verbose

语法讲解:

Update-Database 
-Force                                      #数据丢失被允许
-ProjectName 1-3EntityFrameWorkDemo         #指定 migration configuration 所在项目
-StartUpProjectName 1-3EntityFrameWorkDemo  #指定数据库连接字符串所在项目
-ConfigurationTypeName EntityFrameWorkDemo.Migrations.Configuration   #指定使用的迁移配置类
-ConnectionStringName EmployeeDBContext     #指定数据库连接字符串
-Verbose                                    #显示详细信息

语法

Update-Database 
[-SourceMigration <String>]
[-TargetMigration <String>] 
[-Script] 
[-Force] 
[-ProjectName <String>]
[-StartUpProjectName <String>] 
[-ConfigurationTypeName <String>] 
[-ConnectionStringName <String>] 
[-AppDomainBaseDirectory <String>] 
[<CommonParameters>]
    
还有如下一种命令,[-ConnectionStringName <String>] 的特制化配置。

Update-Database 
[-SourceMigration <String>] 
[-TargetMigration <String>]
[-Script] [-Force]
[-ProjectName <String>]
[-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] 
-ConnectionString <String> 
-ConnectionProviderName <String> 
[-AppDomainBaseDirectory <String>] 
[<CommonParameters>]

说明

Updates the database to the current model by applying pending migrations.
通过应用挂起的数据迁移 将数据库与当前模板同步

-SourceMigration <String>

Only valid with -Script. Specifies the name of a particular migration to use
as the update’s starting point. If omitted, the last applied migration in
the database will be used.
仅对-Script有效。指定特定数据迁移的名称 并将之作为数据更新的起点,如果忽略,那么数据库中最后一次成功迁移将作为起点。

-TargetMigration <String>

Specifies the name of a particular migration to update the database to. If
omitted, the current model will be used.
指定 特定数据迁移的名称 并且将之作为数据更新的目标终点,如果忽略,当前model是目标终点。

-Script [<SwitchParameter>]

Generate a SQL script rather than executing the pending changes directly.
生成一个SQL脚本,而不是直接执行挂起的更改。

-Force [<SwitchParameter>]

Specifies that data loss is acceptable during automatic migration of the
database.
指定 数据丢失是被允许的,在数据自动迁移的过程中。

-ProjectName <String>

Specifies the project that contains the migration configuration type to be
used. If omitted, the default project selected in package manager console
is used.
指定 将要被使用的 数据迁移配置类型 所在的项目,如果忽略,会使用 包管理控制台默认选择的项目

-StartUpProjectName <String>

Specifies the configuration file to use for named connection strings. If
omitted, the specified project’s configuration file is used.
大概意思是说配置连接字符串所在的项目文件? 为连接字符串指定配置文件。如果忽略,默认启动文件中的配置文件将被使用。

-ConfigurationTypeName <String>

Specifies the migrations configuration to use. If omitted, migrations will
attempt to locate a single migrations configuration type in the target
project.
有的时候我们的项目中不止一个数据迁移文件,所以如果你直接去执行三个简单的命令,是会报错的,要指定你到底要用哪一个DBContext。

-ConnectionStringName <String>

Specifies the name of a connection string to use from the application’s
configuration file.
从配置文件中指定我们要是用的 数据库连接字符串的名字

-ConnectionString <String>

Specifies the connection string to use. If omitted, the context’s
default connection will be used.
指定连接字符串

-ConnectionProviderName <String>

Specifies the provider invariant name of the connection string.
指定dataProvider,例如 providerName=“System.Data.SqlClient”

-AppDomainBaseDirectory <String>

Specifies the directory to use for the app-domain that is used for running Migrations
code such that the app-domain is able to find all required assemblies. This is an
advanced option that should only be needed if the solution contains several projects
such that the assemblies needed for the context and configuration are not all
referenced from either the project containing the context or the project containing
the migrations.
指定app-domain的目录,从而迁移运行中可以代码生成,这样app-domain可以找到 所有需要的程序集。这是一个只有在解决方案包含多个项目时才需要的高级选项。从而 context和 configuration 所需要的程序集 ,并不需要全部被 包含context或者 包含migrations的项目引用。

<CommonParameters>

此 Cmdlet 支持常见参数: Verbose、Debug、
ErrorAction、ErrorVariable、WarningAction、WarningVariable、
OutBuffer、PipelineVariable 和 OutVariable。有关详细信息,请参阅
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216)。

    -------------------------- 示例 1 --------------------------
    
PS C:\>Update-Database

    -------------------------- 示例 2 --------------------------
    
PS C:\>Update-Database -TargetMigration Second

    -------------------------- 示例 3 --------------------------
    
PS C:\>Update-Database -Script

    -------------------------- 示例 4 --------------------------

PS C:\>Update-Database -Script -SourceMigration Second -TargetMigration First

    -------------------------- 示例 5 --------------------------

PS C:\>Update-Database -Script -SourceMigration $InitialDatabase

# Generate a script that can upgrade a database currently at any version to the latest version.
# The generated script includes logic to check the __MigrationsHistory table and only apply changes
# that haven't been previously applied.
#生成一个脚本,该脚本可以将当前任何版本的数据库升级到最新版本。
#生成的脚本包含检查 __MigrationsHistory表的逻辑,并且只更改以前没有更改过的

-------------------------- 示例 6 --------------------------

PS C:\>Update-Database -TargetMigration $InitialDatabase

# Runs the Down method to roll-back any migrations that have been applied to the database

备注
若要查看示例,请键入: “get-help Update-Database -examples”.
有关详细信息,请键入: “get-help Update-Database -detailed”.
若要获取技术信息,请键入: “get-help Update-Database -full”.

 类似资料: