Repository navigation
Closed
Labels
Milestone
Description
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
Content source URL
Document Version Independent Id
34bc1bac-7338-d8ab-390d-8c5e88a48c10
Activity
ajcvickers commentedon Feb 14, 2024
Duplicate of #4318
jasekiw commentedon 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 commentedon Feb 14, 2024
@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 commentedon Feb 14, 2024
@ajcvickers Sounds reasonable to me.
hugocubides commentedon Mar 6, 2024
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 commentedon Mar 10, 2024
A better way to handle migrations separately -
https://github.com/k-basant/dotnet-ef-clean-modular-template/tree/main/db/ProjName.DBManager
Expand and clarify migrations guidance. (dotnet#5445)