其他分享
首页 > 其他分享> > 迁移时,EF Core工具错误

迁移时,EF Core工具错误

作者:互联网

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

> WebApi .net core 2.1-Test.Api
>类库.net核心2.1目标框架-Test.Data
>类库.net核心2.1目标框架-Test.Model

在Test.Data中,我创建了一个上下文类,DbInizializer类,存储库,…
在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")));

我也创建此类“ 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(connection,b => b.MigrationsAssembly(“ Test.Api”)).默认情况下,迁移程序集是包含DbContext的程序集.
通过使用程序包管理器控制台的“默认项目”下拉列表,或从包含迁移项目的目录中执行“ dotnet ef”,将目标项目更改为迁移项目.

我已经完成了所有软件包的升级.

我不明白,因为这行不通.

BR

解决方法:

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

关于抱怨版本不匹配,我有相同的错误消息,而this link导致我找到了解决方案.

运行以下命令:

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.1.4

标签:asp-net-core-2-1,asp-net-web-api2,c,entity-framework
来源: https://codeday.me/bug/20191024/1924624.html