-
[EF Core] 마이그레이션 명령 정리, 자주 나는 오류 해결Database 2026. 11. 10. 00:39

EF Core 마이그레이션에서 실제로 쓰는 명령과, 자주 나는 오류 대응을 정리했습니다.
절차
- 도구 설치
- 마이그레이션 추가·적용
- 운영 반영 방식
- 자주 나는 오류
1. 도구 설치
dotnet tool install --global dotnet-ef dotnet add package Microsoft.EntityFrameworkCore.Design설치 후
dotnet ef --version이 안 되면~/.dotnet/tools가 PATH에 있는지 확인합니다.2. 마이그레이션 추가와 적용
dotnet ef migrations add InitialCreate dotnet ef database update프로젝트가 여러 개면 데이터 프로젝트와 시작 프로젝트를 지정합니다.
dotnet ef migrations add AddOrderTable -p ./Data -s ./Web3. 운영 반영
운영에서는
database update를 직접 돌리기보다 SQL 스크립트를 뽑아 검토한 뒤 적용하는 편이 안전합니다.dotnet ef migrations script -o migrate.sql --idempotent4. 자주 나는 오류
No migrations were applied. The database is already up to date.
연결 문자열이 다른 DB를 보거나, 이미 적용된 상태입니다. 이력은
dotnet ef migrations list로 확인합니다.The model backing the context has changed — 모델만 바꾸고 마이그레이션을 만들지 않았습니다.
migrations add후database update.Unable to create a 'DbContext' of type ... — 디자인 타임에 컨텍스트를 만들지 못할 때 납니다. 아래 팩터리를 추가합니다.
public class DesignTimeFactory : IDesignTimeDbContextFactory<AppDbContext> { public AppDbContext CreateDbContext(string[] args) { var options = new DbContextOptionsBuilder<AppDbContext>() .UseSqlServer("Server=localhost;Database=App;Trusted_Connection=True;") .Options; return new AppDbContext(options); } }데이터 손실 경고(SQL Server) — 컬럼 삭제나 형 변환이 있으면 경고가 나옵니다.
-- --force로 넘기기 전에 스크립트로 실제 SQL을 확인하세요.정리
- 추가는
migrations add, 적용은database update - 운영은
migrations script --idempotent로 뽑아 검토 후 적용 - 이력은
migrations list로 확인
'Database' 카테고리의 다른 글
[Database] Linux-Docker-Mysql 데이터베이스 자동 백업 (0) 2021.11.29 [MySQL] 문자 형식 변경 방법(utf8_general_ci) (0) 2021.10.01 [Database] 문자열 합치기 (CONCAT, GROUP_CONCAT) (0) 2020.07.09 [Database] UTC 시간 기준으로 쿼리 값 입력하기 (0) 2020.02.03 [Database] 쿼리문 ( 작성중.... ) (0) 2019.05.09