SQL Server EF Core 2.0 Migration - dotnet ef migrations 'script' command outputting blank file

f3temu5u  于 2023-06-28  发布在  其他
关注(0)|答案(4)|浏览(113)

I have recently migrated my web project to Asp.Net Core 2.0 and Entity Framework Core 2.0.

The web application is working correctly and all dotnet ef migrations CLI commands are working apart from the following:

dotnet ef migrations script {migrationname1} {migrationname1} -o my-file-name.sql

The query appears to run with no errors and then produces no SQL code either to screen or file.

I am using Visual Studio 2017 with SQL Server Express (localdb) on Windows.

Just wondering if anyone else has come across the issue since migrating?

Would appreciate some help as can not find anything on here or Google.

66bbxpm5

66bbxpm51#

After lots of wasted effort and time, it looks like this is a bug with dotnet ef migrations script , as this is not functioning as it did in EF Core 1.0.

Script-Migration -From 20171106124427_061120171243

Outputs blank

Script-Migration -From 20171106124427_061120171243 -To 20171106124427_061120171243

Outputs blank (note the -From and -To values are the same)

Script-Migration 20171106124427_061120171243

Outputs blank

Script-Migration -From 20171106122611_061120171225 -To 20171106124427_061120171243

Outputs SQL statements as expected (Note -From and -To values are different)

I get the same results whether I try this in Visual Studio 2017 Package Manager Console or the CLI.

After further investigation it would appear the answer lies in the following:

Script-Migration -From <PreviousMigration> -To <LastMigration>

It would appear that it is my misunderstanding of the -From and -To attributes that is the problem regardless of whether this used to work in a previous version.

I had incorrectly assumed that the -From migration file would be included in the resulting code generation, but after some testing this does not seem to be the case.

xnifntxz

xnifntxz2#

E. Gads. LOL

I have come up with the answer and it is so ridiculous.

Here is the syntax you use to add a migration:

Add-Migration MigrationName -Context MyDbContext

Because I like to save time I up arrow and down arrow in the Package Manager Console to recall previous commands. If you recall the command and change Add-Migration to Script-Migration you will end up runnig:

Script-Migration MigrationName -Context MyDbContext

This will end up silently failing and opening up an empty SQL file! Drat!

This is the command you want to run:

Script-Migration -Context MyDbContext

Then it will work. If this is not your problem, sorry. I found that adding the -v option to the command is quite revealing and I would suggest that you try it.

EDIT:

Above are PowerShell commands for the Package Manager Console. The answer by @Suraj is helpful if you are using dotnet ef migrations from the command line. (You need to use the FROM and TO arguments).

ocebsuys

ocebsuys3#

dotnet ef migrations script FromA FromB

This will get the script generated in the terminal itself

If you want to generate to a file use this option

dotnet ef migrations script FromA FromB -o sample.sql
fnatzsnv

fnatzsnv4#

Note that if you are creating your initial Script-Migration then leave the migration name blank. Leaving the migration name blank will also script the entire database.

So, for example, this will return a blank sql file in a project with one migration named InitialCreate:

Script-Migration InitialCreate

While this would return the correct sql file:

Script-Migration

相关问题