你如何在netcoreapp中使用`dotnet ef ...`工具?

时间:2017-07-12 02:28:23

标签: .net entity-framework .net-core

我正在使用.net核心的2.0.0预览版本,并且我已经创建了一个netcoreapp2.0应用程序来与实体框架核心一起使用。

.csproj文件是:

<Project Sdk="Microsoft.NET.Sdk">
  <ItemGroup>
    <ProjectReference Include="..\HelloEF.Core\HelloEF.Core.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" />
  </ItemGroup>
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>

调用dotnet restore后,工具似乎安装正确,因为我可以成功使用dotnet ef命令:

$ dotnet ef dbcontext
Usage: dotnet ef dbcontext [options] [command]
...

...但尝试创建迁移失败:

$ dotnet ef migrations add InitialLoad
System.ArgumentNullException: Value cannot be null.
Parameter name: contentRootPath
   at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations..ctor(IOperationReporter reporter, Assembly assembly, Assembly startupAssembly, String environment, String projectDir, String contentRootPath, String rootNamespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<>c__DisplayClass4_0.<.ctor>b__4()
   at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Value cannot be null.
Parameter name: contentRootPath

我知道这是一个预览版本,这可能是一个错误,但我觉得我可能实际上在这里做错了,而不是只是被打破了。

不应该将ContentRootPath检测为从dotnet...调用的路径吗?

我是否需要进行其他类型的设置才能使其正常工作?

围绕这个的所有教程似乎只是假设如果您使用EF核心,那么您也使用ASP.NET核心;但我不是,我只是创建一个命令行工具。

1 个答案:

答案 0 :(得分:1)

错误消息具有误导性。您只是混合不兼容的软件包版本。对所有包使用2.0.0-*或对所有包使用1.1.x-*

<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0-*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0-*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="2.0.0-*" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0-*" />    
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-*" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-*" />