Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 349. dotnet ef 명령어 사용을 위한 준비 [링크 복사], [링크+제목 복사],
조회: 13088
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

dotnet ef 명령어 사용을 위한 준비

dotnet ef 명령어는 Microsoft.EntityFrameworkCore를 위한 확장 명령어인데, 이에 대해서는 다음의 글에서 자세하게 설명하고 있습니다.

EF Core .NET Command-line Tools
; https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

dotnet ef 명령어를 위한 사전 준비 작업은 간단합니다. 우선, .csproj 파일에 다음과 같이 DotNetCliToolReference 노드를 추가합니다.

<Project Sdk="Microsoft.NET.Sdk.Web">

  ...[생략]...
  <ItemGroup>
    ...[생략]...
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>

</Project>

이렇게만 설정하면 이후 dotnet ef 명령어가 실행 가능합니다. 하지만, "dotnet ef database update"와 같은 명령어가 제대로 동작하려면 해당 프로젝트에 "Microsoft.EntityFrameworkCore.Design" 어셈블리도 참조가 되어 있어야 합니다. 이를 위해 다음과 같이 NuGet으로부터 패키지 추가를 하면 됩니다.

Install-Package Microsoft.EntityFrameworkCore.Design

또는 명령행에서 "dotnet add package"를 수행해도 됩니다.

...> dotnet add package Microsoft.EntityFrameworkCore.Design
...> dotnet restore

이후 .csproj 파일이 있는 폴더에서 "dotnet ef ..." 명령어를 수행하면 정상적으로 Microsoft.EntityFrameworkCore 관련 작업을 수행할 수 있습니다.




오류 정리입니다. ^^ 만약 다음과 같이 오류가 발생하면,

E:\WebApplication1> dotnet ef
No executable found matching command "dotnet-ef"

반드시 .csproj 파일과 같은 폴더에서 "dotnet ef" 명령어를 수행했는지 확인 후, .csproj 파일에도 다음의 설정이 포함되어 있는지 봅니다.

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />




반면, 다음과 같은 오류가 발생한다면?

E:\WebApplication1\WebApplication2> dotnet ef database update
Your startup project 'WebApplication2' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

프로젝트(csproj)에 "Microsoft.EntityFrameworkCore.Design" 어셈블리가 참조되어 있는지 확인합니다. 없다면 NuGet으로부터 추가합니다.

Install-Package Microsoft.EntityFrameworkCore.Design




또는 이런 오류 메시지가 나온다면?

E:\WebApplication1\WebApplication2> dotnet ef database update
No DbContext was found in assembly 'WebApplication2'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.

해당 .csproj 파일에 DbContext 관련 클래스가 정의되어 있지 않기 때문입니다. 관련 코드를 먼저 추가한 다음 "dotnet ef database update" 명령어를 수행해야 합니다.




그 외에, UseSqlServer 확장 메서드 사용 시

services.AddDbContext<YourDBContext>(options => options.UseSqlServer(dbConnectionString));

다음과 같은 컴파일 오류가 발생할 수 있습니다.

Error CS1061 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer' and no extension method 'UseSqlServer' accepting a first argument of type 'DbContextOptionsBuilder' could be found (are you missing a using directive or an assembly reference?)

UseSqlServer 확장 메서드는 Microsoft.EntityFrameworkCore.SqlServer 어셈블리에 정의되어 있는데 이를 참조하지 않아서 그런 것입니다. 역시 NuGet을 통해 참조를 하면 정상적으로 빌드가 됩니다.

Install-Package Microsoft.EntityFrameworkCore.SqlServer




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 1/28/2022]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 76  77  78  79  80  81  82  83  84  85  86  [87]  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11463정성태3/15/201812494.NET Framework: 733. 스레드 간의 read/write 시에도 lock이 필요 없는 경우파일 다운로드1
11462정성태3/14/201814333개발 환경 구성: 354. HTTPS 호출에 대한 TLS 설정 확인하는 방법 [1]
11461정성태3/13/201817588오류 유형: 454. 윈도우 업데이트 설치 오류 - 0x800705b4 [1]
11460정성태3/13/201811162디버깅 기술: 112. windbg - 닷넷 메모리 덤프에서 전역 객체의 내용을 조사하는 방법
11459정성태3/13/201810893오류 유형: 453. Debug Diagnostic Tool에서 mscordacwks.dll을 찾지 못하는 문제
11458정성태2/21/201812397오류 유형: 452. This share requires the obsolete SMB1 protocol, which is unsafe and could expose your system to attack. [1]
11457정성태2/17/201817231.NET Framework: 732. C# - Task.ContinueWith 설명 [1]파일 다운로드1
11456정성태2/17/201821927.NET Framework: 731. C# - await을 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법 [7]파일 다운로드1
11455정성태2/17/201812396오류 유형: 451. ASP.NET Core - An error occurred during the compilation of a resource required to process this request.
11454정성태2/12/201820652기타: 71. 만료된 Office 제품 키를 변경하는 방법
11453정성태1/31/201812177오류 유형: 450. Azure Cloud Services(classic) 배포 시 "Certificate with thumbprint ... doesn't exist." 오류 발생
11452정성태1/31/201817532기타: 70. 재현 가능한 최소한의 예제 프로젝트란? [3]파일 다운로드1
11451정성태1/24/201812492디버깅 기술: 111. x86 메모리 덤프 분석 시 닷넷 메서드의 호출 인자 값 확인
11450정성태1/24/201825849Windows: 146. PowerShell로 원격 프로세스(EXE, BAT) 실행하는 방법 [1]
11449정성태1/23/201814860오류 유형: 449. 단위 테스트 - Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.VideoRecorderEngine' or one of its dependencies. [1]
11448정성태1/20/201812029오류 유형: 448. Fakes를 포함한 단위 테스트 프로젝트를 빌드 시 CS0619 관련 오류 발생
11447정성태1/20/201813546.NET Framework: 730. dotnet user-secrets 명령어 [2]파일 다운로드1
11446정성태1/20/201814818.NET Framework: 729. windbg로 살펴보는 GC heap의 Segment 구조 [2]파일 다운로드1
11445정성태1/20/201812091.NET Framework: 728. windbg - 눈으로 확인하는 Workstation GC / Server GC
11444정성태1/19/201813135VS.NET IDE: 125. Visual Studio에서 Selenium WebDriver를 이용한 웹 브라우저 단위 테스트 구성파일 다운로드1
11443정성태1/18/201813012VC++: 124. libuv 모듈 살펴 보기
11442정성태1/18/201811395개발 환경 구성: 353. ASP.NET Core 프로젝트의 "Enable unmanaged code debugging" 옵션 켜는 방법
11441정성태1/18/201810663오류 유형: 447. ASP.NET Core 배포 오류 - Ensure that restore has run and that you have included '...' in the TargetFrameworks for your project.
11440정성태1/17/201813102.NET Framework: 727. ASP.NET의 HttpContext.Current 구현에 대응하는 ASP.NET Core의 IHttpContextAccessor/HttpContextAccessor 사용법파일 다운로드1
11439정성태1/17/201817457기타: 69. C# - CPU 100% 부하 주는 프로그램파일 다운로드1
11438정성태1/17/201812710오류 유형: 446. Error CS0234 The type or namespace name 'ITuple' does not exist in the namespace
... 76  77  78  79  80  81  82  83  84  85  86  [87]  88  89  90  ...