简体   繁体   English

创建迁移时EF Core工具出错

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

I'm experimenting with a solution of 3 projects: 我正在尝试3个项目的解决方案:

  • WebApi .net core 2.1 - Test.Api WebApi .net核心2.1 - Test.Api
  • Class Library .net core 2.1 Target Framework - Test.Data 类库.net核心2.1目标框架 - Test.Data
  • Class Library .net core 2.1 Target Framework - Test.Model 类库.net核心2.1目标框架 - Test.Model

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.

相关问题 创建迁移失败。 EF Core工具早于某些版本 - Failed to create migration. EF Core tools is older than some version 尝试创建 C# EF 核心迁移时出错,System.NullReferenceException: Object 引用未设置为 object 的实例 - Error when trying to create a C# EF core migration, System.NullReferenceException: Object reference not set to an instance of an object 从 EF 迁移到 EF Core 时,是否需要使用整个数据库结构创建新的迁移? - When migrating from EF to EF Core do you need to create a new migration with the entire database structure? EF Core (3.1) 工具:有没有办法将数据库降级到以前的迁移? - EF Core (3.1) tools: is there a way to downgrade a database to just the previous migration? EF Core - 创建没有连接字符串的迁移 - EF Core - Create a migration without connection string 尝试创建初始迁移 EF Core 3 asp.net Core 时出错 - Error while trying to create initial migration EF Core 3 asp.net Core 如何使用 HiLo 解决 EF Core 迁移错误? - How to resolve EF Core migration error with HiLo? EF Core 迁移在使用“ApplyConfiguration”时不起作用 - EF Core Migration not working when using `ApplyConfiguration` ef core 在迁移时迁移数据 - ef core migrate data when migration applies EF Core 迁移错误:PositionalParameterNotFound,Add-Migration - EF Core migration error: PositionalParameterNotFound,Add-Migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM