Reputation: 179
I'm experimenting with a solution of 3 projects:
In Test.Data I created a Context Class, DbInizializer Class, Repository, ... In Test.Model I created my Entities
In Test.Api I want to create the migration and installed this package :
<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
services.AddDbContext<TestContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("Test.Api")));
I create also this 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);
}
}
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'. Update the tools for the latest features and bug fixes. Your target project 'Test.Api' doesn't match your migrations assembly 'Test.Data'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("Test.Api")). By default, the migrations assembly is the assembly containing the 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.
I've done all the package upgrades.
I don't understand because doesn't work.
BR
Upvotes: 3
Views: 3265
Reputation: 2515
You must reference the EF tools and create your migration script in your project where your EF context is - that is 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
Upvotes: 5