我无法 运行 迁移(使用 .net core 3.0 和 entity framework)

I can't run migration (with .net core 3.0 and entity framework)

我的.Net Core 版本是3.0.100-preview6-012264。你可以问我为什么用预览版。主要原因是使用 GRPC 很有用(为此搭建脚手架)。现在我想在我的项目中使用 entity framework 。我有以下 .csproj:

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

    <PropertyGroup>
        <TargetFramework>netcoreapp3.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <Protobuf Include="Protos\dashboard.proto" GrpcServices="Server" Generator="MSBuild:Compile" />
        <Protobuf Include="Protos\users.proto" GrpcServices="Client" Generator="MSBuild:Compile" />
        <Protobuf Include="Protos\anal.proto" GrpcServices="Client" Generator="MSBuild:Compile" />
        <Content Include="@(Protobuf)" />
        <None Remove="@(Protobuf)" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="DimaRabbitLogger" Version="1.3.3" />
        <PackageReference Include="Grpc.AspNetCore.Server" Version="0.1.22-pre1" />
        <PackageReference Include="Google.Protobuf" Version="3.8.0" />
        <PackageReference Include="Grpc.Tools" Version="1.21.0" PrivateAssets="All" />
        <PackageReference Include="Grpc.Core" Version="1.21.0" />

        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0-preview6.19304.10" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview6.19304.10" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview6.19304.10" />
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview5" />

        <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
        <PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />

    </ItemGroup>

</Project>

还有几个非常简单的模型:

型号Application:

public class Application
{
    int ApplicationId { get; set; }

    public string PackageName { get; set; }

    public List<WorkspaceApplication> WorkspaceApplications { get; set; }

型号Workspace:

public class Workspace
{
    public int WorkspaceId { get; set; }

    public string Name { get; set; }

    public List<WorkspaceApplication> WorkspaceApplications { get; set; }

}

Class 对于多对多关系:

public class WorkspaceApplication
{
    public int ApplicationId { get; set; }
    public Application Application { get; set; }

    public string WorkspaceId { get; set; }
    public Workspace Workspace { get; set; }
}

以及我的应用上下文:

public class ApplicationContext : DbContext
{
    private readonly string _connectionString;

    public DbSet<Workspace> Workspaces { get; set; }
    public DbSet<Application> Applications { get; set; }

    public ApplicationContext(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("DashboardDb");
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(_connectionString, b => b.MigrationsAssembly("Dashboard"));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<WorkspaceApplication>()
            .HasKey(x => new {x.WorkspaceId, x.ApplicationId});

        modelBuilder.Entity<WorkspaceApplication>()
            .HasOne(x => x.Workspace)
            .WithMany(x => x.WorkspaceApplications)
            .HasForeignKey(x => x.WorkspaceId);

        modelBuilder.Entity<WorkspaceApplication>()
            .HasOne(x => x.Application)
            .WithMany(x => x.WorkspaceApplications)
            .HasForeignKey(x => x.ApplicationId);
    }
}

我运行以下控制台命令:

dotnet ef migrations add FirstMigration

并收到此错误消息:

Could not execute because the specified command or file was not found. Possible cause: * You have incorrectly typed builtin dotnet. * You planned to run .NET Core, but dotnet-ef does not exist. * You wanted to run the global tool, but the PATH specified in the PATH could not find the executable file with the dotnet prefix, which has this name.

这适用于 .Net Core 2.2。问题出在哪里?

Starting with 3.0 the dotnet ef is not part of the .NET SDK anymore.

Old behavior

Before 3.0, the dotnet ef tool was included in the .NET Core SDK and was readily available to use from the command line from any project without requiring extra steps.

New behavior

Starting in 3.0, the .NET SDK does not include the dotnet ef tool, so before you can use it you have to explicitly install it as a local or global tool.

要安装它,您可以运行这个:

dotnet tool install --global dotnet-ef --version 3.0.0-*

那么你应该能够 运行 正常的 ef-core 命令,比如 dotnet ef migrations add ...

不要忘记在 OS 的 PATH 变量中添加 dotnet 工具的路径。例如,如果你使用 linux ubuntu 和 zsh 作为 shell 你必须在你的 .zshrc 文件中添加以下内容:

export PATH=$PATH:$HOME/.dotnet/tools