簡體   English   中英

創建遷移時EF Core工具出錯

[英]The EF Core tools in error when create the migration

我正在嘗試3個項目的解決方案:

  • WebApi .net核心2.1 - Test.Api
  • 類庫.net核心2.1目標框架 - Test.Data
  • 類庫.net核心2.1目標框架 - Test.Model

在Test.Data中我創建了一個Context類,DbInizializer類,Repository,...在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。

我有關於抱怨版本不匹配的相同錯誤消息, 這個鏈接引導我解決方案。

運行以下命令:

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 2.1.4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM