简体   繁体   中英

How can I use a DbContext from a different assembly for my migrations?

I have multiple projects in a solution. For clean architecture reasons, I want to keep the DbContext in Infrastructure project, but my Startup Project is the API project. So the Program.cs is in the API project.

I tried to specify that I wanted to use my context from the Infrastructure proect like this:

builder.Services.AddDbContext<DzbcDbContext>(opt => opt.UseSqlServer(
     builder.Configuration.GetConnectionString("DefaultConnection"),
     b => b.MigrationsAssembly("DZBC.Infrastructure") )); // <- tried this

But it's not working. The error still tells me it's looking for the DbCOntext in the API project.

All similar answers are either old or don't work in .net core 6.

It's solved by forcing it in Developer PowerShell.

Navigate to the project folder where you have your DbContext (in my case Infrastructure). From there use this:

dotnet ef migrations add initial --startup-project "../DZBC.API/DZBC.API.csproj"
dotnet ef database update --startup-project "../DZBC.API/DZBC.API.csproj"

But use the .csproj path of your Startup project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM