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

.net-core-3.0 ef-migrations entity-framework-core

Question

My .Net Core version is 3.0.100-preview6-012264. You can ask me why I use the preview version. The main reason is useful work with GRPC (scaffolding for this). Now I would like to use entity framework in my project. I have the following .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>

And couple of very simple models:

Model Application:

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

    public string PackageName { get; set; }

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

Model Workspace:

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

    public string Name { get; set; }

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

}

Class for many-to-many relationship:

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

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

And my application context:

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);
    }
}

I run the following console command:

dotnet ef migrations add FirstMigration

and have this error message:

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.

This works nice with .Net Core 2.2. Where is the problem?

1
1
8/7/2019 1:04:46 PM

Accepted Answer

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.

To install it, you can run this:

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

Then you should be able to run normal ef-core commands like dotnet ef migrations add ...

Don't forget to add the path to dotnet tools in the PATH variable of your OS. For example if you use linux ubuntu and zsh as shell you have to add the following int your .zshrc file:

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

5
8/7/2019 2:53:24 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow