且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架6(EF6)使用单独项目中的模型进行首次迁移

更新时间:2023-02-15 20:05:53

您可以传递额外的参数到 update-database ,允许:

You can pass extra parameters to update-database that allow to:


  • 指定不同的连接字符串

  • 生成一个可以自己应用到目标数据库的SQL脚本

完整语法更新数据库

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

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

指定连接字符串(指向项目中正确的文件夹) ):

To specify a connection string (which points to the correct folder in your project):


  • -StartUpProjectName -ConnectionStringName ,以指定包含.config文件的项目,其中带有提供名称的连接字符串(第一个语法)

  • -ConnectionString ,直接指定连接字符串(第二个语法)

  • -StartUpProjectName and -ConnectionStringName, to specify a project that contains a .config file with a connection string with the provided name (1st syntax)
  • -ConnectionString, to specify directly the connection string (2nd syntax)

要创建一个SQL脚本,您可以直接应用于DB使用其他工具:

To create a SQL script that you can apply directly to the DB using other tool:


  • -SourceMigration :目标数据库中的当前迁移 -TargetMigration :迁移到更新到,和 -Script :生成一个可以手动应用的脚本taht

  • -SourceMigration: current migration in the destination DB -TargetMigration: migration to update to, and -Script: to generate a script taht you can apply by hand