EF Core Reverse Engineering

Reverse Engineering


리버스 엔지니어링은 데이터베이스 스키마에 기반 하 여 엔터티 형식 클래스 및 DbContext 클래스를 스캐폴딩 하는 프로세스이다. 아래 두가지 방법을 이용할 수 있다.

Scaffold-DbContextPMC  : (EF Core 패키지 관리자 Console) 도구 

dotnet ef dbcontext scaffold : .net CLI (명령줄 인터페이스) 도구


1. 개발자 명령 프롬프트로 이동한다. 

2. 솔루션 파일이 아닌 project (.csproj) 파일이 있는 디렉토리로 이동한다. 

3. 다음 명령을 실행한다. 

dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=WebApiServer" Microsoft.EntityFrameworkCore.SqlServer --context-dir Infrastructure/Reverse --output-dir Infrastructure/Reverse/Models


4. 아래와 같이 User.cs 모델과 WebApiServerContext.cs 라는 DB Context 가 생성된다. 



5. 원래 존재 하던 MyDbContext.cs 를 삭제 하자.

6. WebApiServerContext.cs 파일을 열고 다음을 수정하자.

 OnConfiguraing 을 주석처리

//        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//        {
//            if (!optionsBuilder.IsConfigured)
//            {
//#warning To protect potentially sensitive information in your connection string, you should move it out of source
//                optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=WebApiServer");
//            }
//        }

namespace 변경

namespace WebApiBasicEmpty.Infrastructure

7. Models / User.cs 삭제

8. Reverse / Models / User.cs ==> Models / User.cs 로 이동

9. Models / User.cs 파일을 열고 다음을 수정하자.

namespace 변경

namespace WebApiBasicEmpty.Infrastructure.Models


9. Reverse / WebApiServerContext.cs ==> Infrastructure / WebApiServerContext.cs 로 이동


10. WebApiServerContext.cs ==> MyDbContext.cs 로 이름 변경


11. MyDbContext.cs 파일  열고 다음을 수정하자.

using WebApiBasicEmpty.Infrastructure.Models;


12.  WebApiServer  DB 에서 삭제






13. Migrations 폴더 삭제


14. 개발자 명령 프롬프트로 이동

솔루션 디렉토리로 이동

cd ..

Migration

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

DB Update

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


15. SQL Server Object Explorer 에서 DB 확인





관련영상

https://youtu.be/8agjZCmvUUg








댓글

이 블로그의 인기 게시물

Mediator

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

Dependency Injection Customization