![](/img/trans.png)
[英]Create and Apply Migration at run time in .Net Core - Entity Framework Core
[英]I can't run migration (with .net core 3.0 and entity framework)
我的.Net Core版本是3.0.100-preview6-012264
。 您可以問我為什么使用預覽版。 主要原因是使用GRPC有用(為此是腳手架)。 現在,我想在項目中使用實體框架。 我有以下.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; }
}
多對多關系類:
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
並顯示以下錯誤消息:
由於找不到指定的命令或文件,因此無法執行。 可能的原因:*您輸入的內置dotnet錯誤。 *您計划運行.NET Core,但dotnet-ef不存在。 *您想運行全局工具,但是PATH中指定的PATH找不到帶有dotnet前綴(具有此名稱)的可執行文件。
NET Core 2.2可以很好地工作。 問題出在哪兒?
從3.0開始, dotnet ef
不再是.NET SDK的一部分。
舊行為
在3.0之前的版本中,.net Core SDK中包含了dotnet ef工具,該工具可從任何項目的命令行中輕松使用,而無需執行額外的步驟。
新行為
從3.0開始,.NET SDK不包含dotnet ef工具,因此在使用它之前,必須將其顯式安裝為本地或全局工具。
要安裝它,您可以運行以下命令:
dotnet tool install --global dotnet-ef --version 3.0.0-*
然后,您應該能夠運行普通的ef-core命令,例如dotnet ef migrations add ...
不要忘記在您操作系統的PATH
變量中添加dotnet工具的PATH
。 例如,如果您使用linux ubuntu和zsh作為外殼,則必須在.zshrc
文件中添加以下內容:
export PATH=$PATH:$HOME/.dotnet/tools
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.