gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:55:48 25 4
gpt4 key购买 nike

我的 .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>

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

模型应用:

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

public string PackageName { get; set; }

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

模型工作区:

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

public string Name { get; set; }

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

}

多对多关系类:

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 添加 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 工具安装 --global dotnet-ef --version 3.0.0-*

然后您应该能够运行正常的 ef-core 命令,例如 dotnet ef migrations add ...

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

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

关于entity-framework-core - 我无法运行迁移(使用 .net core 3.0 和 Entity Framework ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57393678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com