Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Migrations Project - does not work #4650

Open
jasekiw opened this issue Feb 12, 2024 · 6 comments
Open

Separate Migrations Project - does not work #4650

jasekiw opened this issue Feb 12, 2024 · 6 comments

Comments

@jasekiw
Copy link

jasekiw commented Feb 12, 2024

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

@ajcvickers
Copy link
Member

Duplicate of #4318

@ajcvickers ajcvickers marked this as a duplicate of #4318 Feb 14, 2024
@jasekiw
Copy link
Author

jasekiw commented Feb 14, 2024

@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?

@ajcvickers
Copy link
Member

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

@jasekiw
Copy link
Author

jasekiw commented Feb 14, 2024

@ajcvickers Sounds reasonable to me.

@ajcvickers ajcvickers added this to the Backlog milestone Feb 28, 2024
@hugocubides
Copy link

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.

@k-basant
Copy link

A better way to handle migrations separately -

https://github.com/k-basant/dotnet-ef-clean-modular-template/tree/main/db/ProjName.DBManager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants