问题描述
在 Entity Framework 6 中,我需要获取特定迁移文件的 SQL 脚本,并且我已经更新了数据库.我发现在 update-database
命令中我可以添加脚本参数以将迁移导出到 SQL,但我已经更新了数据库.我在 Entity Framework Core 中找到了一个名为 Script-Migration
的命令,其中脚本名称作为参数.实体框架中是否有类似的命令?
In Entity Framework 6, I need to get the SQL script for specific migration file and I already updated the database. I found that in update-database
command I can add script parameter to export the migration to SQL, but I already updated the database. I found in Entity Framework Core, a command called Script-Migration
with script name as an argument. Is there any similar command in Entity framework?
我也尝试了建议更新数据库命令",但我得到了一个空的SQL 文件.
I also tried the suggestion "Update database command", but I got an empty SQL file.
推荐答案
是的,你可以生成migration-SQL如下:
Yes, you could generate the migration-SQL as follows:
Update-Database -Script -SourceMigration: <pointFromWichYouWantToStartWithGeneration> -TargetMigration: <pointWhereToEndWithGeneration>
要为所有迁移创建脚本,请执行以下操作:
To create a script for all migrations execute the following:
Update-Database -Script -SourceMigration: $InitialDatabase
它不会将更改应用到数据库,而是生成一个 SQL 脚本文件.因此,即使迁移已应用于数据库,您也可以生成 SQL 脚本.
Instead of applying the changes to database it will generate a SQL script file. Therefore you could generate a SQL Script even if the migration was already applied to the database.
在这里您可以找到更多关于它的信息 - 实体框架代码优先迁移 - 获取 SQL 脚本.
Here you could find some more information about it - Entity Framework Code First Migrations - Getting a SQL Script.
运行 Update-Database
命令,但这次指定 –Script
标志,以便将更改写入脚本而不是应用.我们还将指定要为其生成脚本的源迁移和目标迁移.
这篇关于在 Entity Framework 6 C# 中获取特定迁移的 SQL 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!