[英]The EF Core tools in error when create the migration
I'm experimenting with a solution of 3 projects: 我正在尝试3个项目的解决方案:
In Test.Data I created a Context Class, DbInizializer Class, Repository, ... In Test.Model I created my Entities 在Test.Data中我创建了一个Context类,DbInizializer类,Repository,...在Test.Model中我创建了我的实体
In Test.Api I want to create the migration and installed this package : 在Test.Api中我想创建迁移并安装此包:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\images\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" />
<PackageReference Include="AutoMapper.Data" Version="2.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="5.0.1" />
<PackageReference Include="FluentValidation" Version="8.0.100" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Test.Data\Test.Data.csproj" />
<ProjectReference Include="..\Test.Model\Test.Model.csproj" />
</ItemGroup>
</Project>
In Startup.cs add this code 在Startup.cs中添加此代码
services.AddDbContext<TestContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("Test.Api")));
I create also this class "DesignTimeDbContextFactory" 我也创建了这个类“DesignTimeDbContextFactory”
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<TestContext>
{
public TestContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<TestContext>();
var connectionString = configuration.GetConnectionString("DefaultConnection");
//configuration["Data:Products:ConnectionString"];
//
builder.UseSqlServer(connectionString);
return new TimeShareContext(builder.Options);
}
}
When I run this 当我跑这个
dotnet ef migrations add "InitialCreate" -o "Data\Migrations"
Obtain this 获得这个
The EF Core tools version '2.1.3-rtm-32065' is older than that of the runtime '2.1.4-rtm-31024'. EF Core工具版本“2.1.3-rtm-32065”早于运行时“2.1.4-rtm-31024”。 Update the tools for the latest features and bug fixes.
更新用于最新功能和错误修复的工具。 Your target project 'Test.Api' doesn't match your migrations assembly 'Test.Data'.
您的目标项目“Test.Api”与迁移程序集“Test.Data”不匹配。 Either change your target project or change your migrations assembly.
更改目标项目或更改迁移程序集。
Change your migrations assembly by using DbContextOptionsBuilder. 使用DbContextOptionsBuilder更改迁移程序集。 Eg options.UseSqlServer(connection, b => b.MigrationsAssembly("Test.Api")).
例如options.UseSqlServer(connection,b => b.MigrationsAssembly(“Test.Api”))。 By default, the migrations assembly is the assembly containing the DbContext.
默认情况下,迁移程序集是包含DbContext的程序集。 Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.
使用程序包管理器控制台的“默认项目”下拉列表或从包含迁移项目的目录执行“dotnet ef”,将目标项目更改为迁移项目。
I've done all the package upgrades. 我已完成所有包升级。
I don't understand because doesn't work. 我不明白,因为不起作用。
BR BR
You must reference the EF tools and create your migration script in your project where your EF context is - that is Test.Data. 您必须引用EF工具并在您的EF上下文所在的项目中创建迁移脚本 - 即Test.Data。
I had the same error message about complaining a version mismatch and this link led me to the solution. 我有关于抱怨版本不匹配的相同错误消息, 这个链接引导我解决方案。
run the following command: 运行以下命令:
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.1.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.