欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

ef数据迁移命令总结之Add-Migration 有大用

ef数据迁移命令总结之Add-Migration

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


个人感觉有一篇好的文章,http://www.mortenanderson.net/code-first-migrations-for-entity-framework ,

在本人的感觉就是 其对这些命令的掌握很深刻,所以在项目中摘录好的地方进行翻译,如果侵权,请联系删除。




Add-Migration 

-Name LearningMpaAbp.EntityFramework 

-Force 

-ProjectName LearningMpaAbp.EntityFramework  

-StartUpProjectName ConsoleApp1  

-ConfigurationTypeName  LearningMpaAbp.Migrations.Configuration 

-ConnectionStringName Default  



Add-Migration 

-Name LearningMpaAbp.EntityFramework 

-Force 

-ProjectName LearningMpaAbp.EntityFramework  

-StartUpProjectName ConsoleApp1  

-ConfigurationTypeName  LearningMpaAbp.Migrations.Configuration 

<!-- 如下和上边的 -ConnectionStringName Default  的作用是一样的 -->

-ConnectionString "data source=DESKTOP-H6LD3FA\SQLEXPRESS;Database=LearningMpaAbp;User Id=sa;Password=123456;" 

-ConnectionProviderName System.Data.SqlClient



1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20


名称Add-Migration

The add-migration command is one of the key commands in code first migrations. When you make changes to your domain model and need them dded into your database you create a new migration. This is done with the Add-Migration command. In it’s simplest form you need only toprovide a migration name.


add-migration命令是code first migration中的关键命令之一。当您对领域域模型进行更改并需要将它们时添加到数据库中,您将创建一个新的迁移。这是通过Add-Migration命令完成的。用最简单的形式,你只需要提供迁移名称。


The Add-Migration command scaffolds your changes into a cs file. This cs file is placed in the same folder as the configuration file for the DbContext you are targeting.


Add-Migration命令将您的更改构建到一个cs文件中。这个cs文件与配置文件放在同一个文件夹中,服务于您要瞄准的DbContext。


摘要

Scaffolds a migration script for any pending model changes.

为任何待定的模型更改 搭建迁移脚本。


语法


    Add-Migration

     [-Name] <String> 

     [-Force] [-ProjectName <String>] 

     [-StartUpProjectName <String>] 

     [-ConfigurationTypeName <String>] 

     [-ConnectionStringName <String>] 

     [-IgnoreChanges] 

     [-AppDomainBaseDirectory <String>]

      [<CommonParameters>]

1

2

3

4

5

6

7

8

9

   Add-Migration 

    [-Force] [-ProjectName <String>]

    [-StartUpProjectName <String>]

    [-ConfigurationTypeName <String>] 

     -ConnectionString <String> 

    [-Name] <String> 

     -ConnectionProviderName <String> 

    [-IgnoreChanges] 

    [-AppDomainBaseDirectory <String>] 

    [<CommonParameters>]    

1

2

3

4

5

6

7

8

9

10

说明

Scaffolds a new migration script and adds it to the project.

搭建一个新的数据迁移脚本 ,并把它添加到项目中。


参数

-Name


Specifies the name of the custom script.


指定自定义脚本 的名字


-Force []


Specifies that the migration user code be overwritten when re-scaffolding an existing migration.


指定 当我们重新 搭建一个已经存在的数据迁移的时候,原本存在的数据迁移代码可以被重写


If you want to re-scaffold an existing migration you will have to use the -Force parameter. However you can only re-scaffold if the migration is not yet applied to database. Else you will need to revert back to the migration previous to the one you want to re-scaffold.


如果要重新构建现有迁移,必须使用-Force参数。然而,只有在迁移尚未应用到数据库时,才能重新构建框架。否则你 需要回复到要重新构建的迁移之前的迁移。


-ProjectName


Specifies the project that contains the migration configuration type to beused. If omitted, the default project selected in package manager console is used.


指定 包含了数据迁移配置类型的项目,如果被忽略,就会选择包管理控制器中的默认项目。


If your migrations should not be placed in your default project you can utilize the -projectname parameter


-StartUpProjectName


Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.


这个其实我也看不懂, 但实验之后,大概是从 解决方案资源管理器中选择一个项目 作为启动项目。如果我们忽略的话,就会默认为解决方案资源管理器中的启动项目。


-ConfigurationTypeName


Specifies the migrations configuration to use. If omitted, migrations willattempt to locate a single migrations configuration type in the target project.


这一段英文解释了,大概还是不会懂。


If you have multiple DbContext in your project you will need to indicate which is going to have the database update. This can be done with -ConfigurationTypeName. The ConfigurationTypeName is the name of your Configuration class in your migration folder.


如果您的项目中有多个DbContext,那么您需要指出哪个数据库会更新。这可以用-ConfigurationTypeName做。ConfigurationTypeName方法是迁移文件夹中配置类的名称。


然后是一个多个DbContext的例子

  First step

  ==========

  

  enable-migrations -ContextTypeName IdentityDb -MigrationsDirectory DAL\IdentityMigrations

  

  enable-migrations -ContextTypeName SMSContext -MigrationsDirectory DAL\SMSMigrations

  

  Second Step

  ===========

  

  add-migration -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration "InitialCreate"

  

  add-migration -ConfigurationTypeName SMSApp.DAL.SMSMigrations.Configuration "InitialCreate"

  

  Third Step

  ==========

  

  update-database -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration -verbose

  

  update-database -ConfigurationTypeName SMSApp.DAL.SMSMigrations.Configuration -verbose

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

-ConnectionStringName


Specifies the name of a connection string to use from the application’s configuration file.


从应用程序的配置文件中指定要使用的连接字符串的名字。我们用两个参数-ConnectionString -ConnectionProviderName ,或者用这样一个参数代替


-ConnectionString


Specifies the the connection string to use. If omitted, the context’s default connection will be used.


指定要使用的连接字符串。如果省略,上将使用DbContext的默认连接。

例如"data source=DESKTOP-H6LD3FA\SQLEXPRESS;Database=LearningMpaAbp;User Id=sa;Password=123456;"


-ConnectionProviderName


指定ConnectionProviderName 例如 System.Data.SqlClient


-IgnoreChanges []


Scaffolds an empty migration ignoring any pending changes detected in the current model.This can be used to create an initial, empty migration to enable Migrations for an existing database. N.B. Doing this assumes that the target database schema is compatible with the current model.

这样做假设目标数据库模式与当前的模型是一致的。构建一个空迁移和对应的空的迁移文件,忽略在当前模型中检测到的任何挂起的更改。可用于创建初始的空迁移,以支持对现有数据库的迁移。


-AppDomainBaseDirectory


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的项目中获取。

————————————————

版权声明:本文为CSDN博主「一头小驴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_37326058/article/details/82928786


来自  https://blog.csdn.net/qq_37326058/article/details/82928786


普通分类: