Programing

EF Core를 사용하여 ASP.NET Core에서 마이그레이션을 적용 취소하는 방법

crosscheck 2020. 6. 29. 08:06
반응형

EF Core를 사용하여 ASP.NET Core에서 마이그레이션을 적용 취소하는 방법


PM> Remove-Migration -context BloggingContextEF Core를 사용하여 ASP.NET Core 프로젝트로 VS2015에서 실행할 때 다음 오류가 발생합니다.

System.InvalidOperationException: The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force) 
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
 The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.

어떻게 적용을 취소 할 수 있습니까? ASP.NET Core 1.0, EF Core 및 VS2015 Update 3의 최신 릴리스를 사용하고 있습니다.


사용하다:

CLI

> dotnet ef database update <previous-migration-name>

패키지 관리자 콘솔

PM> Update-Database <previous-migration-name>

예:

PM> Update-Database MyInitialMigration

그런 다음 마지막 마이그레이션을 제거하십시오.

데이터베이스에 변경 사항을 적용했기 때문에 데이터베이스 업데이트없이 마이그레이션을 제거 할 수 없습니다.

PMC를 사용하는 경우 시도 : PM> update-database 0 데이터베이스를 지우고 솔루션에서 마이그레이션 스냅 샷을 제거 할 수 있습니다.


모든 마이그레이션을 완전히 제거하고 다시 시작하려면 다음을 수행하십시오.

dotnet ef database update 0
dotnet ef migrations remove

여전히 Update-Database명령을 사용할 수 있습니다 .

Update-Database -Migration <migration name> -Context <context name>

그러나 마이그레이션 이름으로 판단하면 명령이 작동하지 않을 수있는 첫 번째 마이그레이션이라고 가정합니다. __MigrationHistory데이터베이스 테이블에서 항목을 삭제 한 후 Remove-Migration명령을 다시 실행할 수 있어야 합니다. 마이그레이션 파일을 삭제하고 다시 시작할 수도 있습니다.


마지막으로 적용된 마이그레이션을 되돌리려면 다음을 수행해야합니다 (패키지 관리자 콘솔 명령).

  1. 데이터베이스에서 마이그레이션을 되 돌리십시오. PM> Update-Database <prior-migration-name>
  2. 프로젝트에서 마이그레이션 파일을 제거하십시오 (또는 다음 단계에서 다시 적용됨).
  3. 모델 스냅 샷 업데이트 : PM> Remove-Migration

UPD : 최신 버전의 Visual Studio (2017)에는 두 번째 단계가 필요하지 않은 것 같습니다.


가치별로 마이그레이션을 타겟팅 할 수 있습니다.

 Update-Database -Migration:0

그런 다음 제거하십시오

 Remove-Migration

특정 마이그레이션을 적용하지 않으려면 다음을 수행하십시오.

dotnet ef database update <previous-migration-name>
or
PM> Update-Database -Migration <previous-migration-name>

모든 마이그레이션을 적용 취소하려면 :

dotnet ef database update 0
or
PM> Update-Database -Migration 0

마지막 마이그레이션을 제거하려면

dotnet ef migrations remove
or
PM> Remove-Migration

마지막 마이그레이션을 적용하고 제거하려면

dotnet ef migrations remove --force
or
PM> Remove-Migration -Force

데이터베이스에 이미 적용된 후 가장 최근의 마이그레이션을 "적용하지 않으려면":

  1. SQL Server 개체 탐색기를 엽니 다 (보기-> "SQL Server 개체 탐색기").
  2. 작은 삼각형을 옆으로 확장하여 프로젝트에 연결된 데이터베이스로 이동하십시오.
  3. "테이블"펼치기
  4. "dbo._EFMigrationsHistory"라는 테이블을 찾으십시오.
  5. 마우스 오른쪽 단추로 클릭하고 "데이터보기"를 선택하여 Visual Studio에서 테이블 항목을보십시오.
  6. 적용을 취소하려는 마이그레이션에 해당하는 행을 삭제하십시오 (프롬프트가 표시되면 "예"라고 경고하십시오).
  7. project.json 파일이있는 디렉토리의 명령 창에서 "dotnet ef migrations remove"를 다시 실행하십시오. 또는 패키지 관리자 콘솔에서 "마이그레이션 제거"명령을 실행하십시오.

Hope this helps and is applicable to any migration in the project... I tested this out only to the most recent migration...

Happy coding!


In Package Manager Console:

Update-Database Your_Migration_You_Want_To_Revert_To

More options and explanation on how to revert migrations can be seen here


In general if you are using the Package Manager Console the right way to remove a specific Migration is by referencing the name of the migration

Update-Database -Migration {Name of Migration} -Context {context}

Another way to remove the last migration you have applied according to the docs is by using the command:

dotnet ef migrations remove

This command should be executed from the developer command prompt (how to open command prompt) inside your solution directory.

For example if your application is inside name "Application" and is in the folder c:\Projects. Then your path should be:

C:\Projects\Application

You Should Delete migration '20160703192724_MyFirstMigration' Record From '_EFMigrationsHistory' Table.

Otherwise This Command will Remove Migration and Delete Migrations Folder:

   > remove-migration -force

In order to unapply a migration in EF Core 1.0 use the command:

dotnet ef database update {migration_name}

Use the migration name of the migration until which you would like to preserve your changes. The list of names of the migration can be found using:

dotnet ef migrations list


var context = serviceProvider.GetRequiredService<ApplicationDbContext>();
var userManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleManaget = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

await context.Database.EnsureDeletedAsync();

1.find table "dbo._EFMigrationsHistory", then delete the migration record that you want to remove. 2. run "remove-migration" in PM(Package Manager Console). Works for me.


at first run the following command :

PM>update-database -migration:0

and then run this one :

PM>remove_migration

Finish

참고URL : https://stackoverflow.com/questions/38192450/how-to-unapply-a-migration-in-asp-net-core-with-ef-core

반응형