创建迁移时 EF Core 工具出错

The EF Core tools in error when create the migration

我正在试验 3 个项目的解决方案:

在 Test.Data 中,我创建了一个上下文 Class、DbInizializer Class、存储库,... 在 Test.Model 我创建了我的实体

在 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>

在Startup.cs中添加这段代码

services.AddDbContext<TestContext>(options =>                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                b => b.MigrationsAssembly("Test.Api")));

我也创建了这个class"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);
    }
}

当我运行这个

dotnet ef migrations add "InitialCreate" -o "Data\Migrations"

获得这个

EF Core 工具版本“2.1.3-rtm-32065”早于 运行时间“2.1.4-rtm-31024”。更新工具以获取最新功能和错误修复。 您的目标项目 'Test.Api' 与您的迁移程序集 'Test.Data' 不匹配。更改目标项目或更改迁移程序集。

使用 DbContextOptionsBuilder 更改迁移程序集。例如。 options.UseSqlServer(连接,b => b.MigrationsAssembly("Test.Api"))。默认情况下,迁移程序集是包含 DbContext 的程序集。 通过使用包管理器控制台的默认项目下拉列表,或从包含迁移项目的目录中执行 "dotnet ef",将目标项目更改为迁移项目。

我已经完成了所有的包升级。

我不明白,因为不起作用。

BR

您必须引用 EF 工具并在您的 EF 上下文所在的项目中创建迁移脚本 - 即 Test.Data。

我收到了关于抱怨版本不匹配的相同错误消息, 引导我找到了解决方案。

运行 以下命令:

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.1.4