EF core migration update

 Migration Update


Model 을 수정하여  field 의  add, remove, update  를 하게 되었을때
해당 내용을 DB 상에 적용 하려면 어떻게 해야 할까?
이러한 내용을 위해 EF Core 는 migration 을 추가 하여 처리 한다. 
그 과정을 알아보자. 


1. Field 추가 및 Db 적용

Infrastructure --> Models --> User.cs 파일을 열고
아래와 같이 변경하자 (No 추가)

public class User
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public int No { get; set; }
}


개발자 명령 프롬프트에서 Migration 을 추가 하자

dotnet ef migrations add AddNo --context MyDbContext --output-dir Migrations/MsSqlMigrations --project WebApiBasicEmpty --startup-project WebApiBasicEmpty

Database 에 적용하자.

dotnet ef database update --context MyDbContext --project WebApiBasicEmpty --startup-project WebApiBasicEmpty


DB 확인




2. Remove Migration


Infrastructure --> Models --> User.cs 파일을 열고
아래와 같이 변경하자 (No제거)

public class User
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
}


개발자 명령 프롬프트에서 Migration 을 추가 하자

dotnet ef migrations add RemoveNo --context MyDbContext --output-dir Migrations/MsSqlMigrations --project WebApiBasicEmpty --startup-project WebApiBasicEmpty


Database 에 update 하지 말고 remove 해보자

dotnet ef migrations remove --context MyDbContext --project WebApiBasicEmpty --startup-project WebApiBasicEmpty


** 주의 **
migration 이후 update database 를 실행 했다면
되돌릴 수 없으니 주의 하자.
개발 중에만 사용하자


관련영상



























댓글

이 블로그의 인기 게시물

Mediator

ASPNET 6 Web Api Basic Tutorial 1 / 2 (Swagger, SeriLog, MediatR, EntityFrameworkCore, Scrutor)

Dependency Injection Customization