Ale*_*bak 2 entity-framework-core entity-framework-migrations .net-core-3.0
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>
Run Code Online (Sandbox Code Playgroud)
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; }
Run Code Online (Sandbox Code Playgroud)
Model Workspace:
public class Workspace
{
public int WorkspaceId { get; set; }
public string Name { get; set; }
public List<WorkspaceApplication> WorkspaceApplications { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
I run the following console command:
dotnet ef migrations add FirstMigration
and have this error message:
无法执行,因为找不到指定的命令或文件。可能的原因: * 您输入的内置 dotnet 有误。* 您计划运行 .NET Core,但 dotnet-ef 不存在。* 您想运行全局工具,但在 PATH 中指定的 PATH 找不到具有此名称的带有 dotnet 前缀的可执行文件。
这适用于 .Net Core 2.2。问题出在哪儿?
从 3.0 开始,dotnet ef它不再是 .NET SDK 的一部分。
旧行为
在 3.0 之前,dotnet ef 工具包含在 .NET Core SDK 中,可随时从任何项目的命令行使用,无需额外步骤。
新行为
从 3.0 开始,.NET SDK 不包含 dotnet ef 工具,因此在使用它之前,您必须将其显式安装为本地或全局工具。
要安装它,您可以运行以下命令:
dotnet tool install --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
| 归档时间: |
|
| 查看次数: |
4034 次 |
| 最近记录: |