Skip to content

Separate Migrations Project - does not work #4650

Closed

Description

@jasekiw

Type of issue

Code doesn't work

Description

I cloned the sample code and ran the command below that the documentation says should work and I get the following error:

 dotnet ef migrations add NewMigration --project WebApplication1.Migrations

Error:

Your startup project 'WebApplication1.Migrations' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

So I add the dependency. and try again. Now I get

Unable to create a 'DbContext' of type ''. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[WebApplication1.Data.ApplicationDbContext]' while attempting to activate 'WebApplication1.Data.ApplicationDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I Then added a design time factory:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using WebApplication1.Data;

namespace WebApplication1.Migrations;

public class ApplicationDBContextFactory:  IDesignTimeDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext CreateDbContext(string[] args)
    {
        var options = new DbContextOptionsBuilder<ApplicationDbContext>();
        options.UseSqlServer(
            "Server=(localdb)\\\\mssqllocaldb;Database=aspnet-WebApplication1-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
            x => x.MigrationsAssembly("WebApplication1.Migrations"));
        return new ApplicationDbContext(options.Options);
    }
}

This finally worked. Something else to note is that If the migrations are in a migrations folder under the migrations project, each migration re-adds the entire database even though the snapshot is there (using pomelo mysql). I think that it should be noted that migrations not directly in the root of the migrations project does not work correctly.

Page URL

https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/managing-schemas/migrations/projects.md

Document Version Independent Id

34bc1bac-7338-d8ab-390d-8c5e88a48c10

Article author

@bricelam

Activity

  1. ajcvickers commented on Feb 14, 2024

    @ajcvickers
    Contributor

    Duplicate of #4318

  2. jasekiw commented on Feb 14, 2024

    @jasekiw
    Author

    @ajcvickers I'm not sure this is a duplicate of that other issue. It seems like a different problem on the same document page. I updated my post because I realize I made some mistakes in there that removed clarity. Can you review my update?

  3. ajcvickers commented on Feb 14, 2024

    @ajcvickers
    Contributor

    @jasekiw You may be right, and I can keep this open. But the main point is that that document needs updating across the board.

  4. jasekiw commented on Feb 14, 2024

    @jasekiw
    Author

    @ajcvickers Sounds reasonable to me.

  5. added this to the Backlog milestone on Feb 28, 2024
  6. hugocubides commented on Mar 6, 2024

    @hugocubides

    Cuando intente crear el controlador desde la interfaz, haga clic en el campo Clase DbContext (+), cree un nuevo contexto y con él haga la creación del controlador. Todo funcionará, posteriormente haga el cambio desde el código apuntando al anterior contexto (el que da problema)... todo funcionará. Pero no podrá crear controladores con su propio contexto.

  7. k-basant commented on Mar 10, 2024

    @k-basant
  8. added a commit that references this issue on Aug 11, 2026
    bb42937
  9. modified the milestones: Backlog, 11.0.0 on Aug 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions