Add-Migration找到DbContext后挂起无响应问题求助
I see you're dealing with a really frustrating problem where running Add-Migration owner -Verbose hangs indefinitely right after successfully locating your MyContext DbContext. The fact that previous migrations worked fine and this is a console app (so we can ignore the hosting-related warnings) makes this even more confusing, especially after waiting over an hour with no progress.
Let's walk through some targeted fixes that often resolve this kind of hang:
Possible Solutions to Try
1. Audit Your DbContext for Blocking Code
EF Core executes your DbContext's constructor and OnConfiguring method when generating migrations. If there's any code here that waits on external resources—like a network call, file lock, or even accidental user input prompts—it can cause the process to freeze.
- Double-check your
MyContextclass for synchronous blocking calls, infinite loops, or code that might be waiting on something that never completes. - Temporarily comment out non-essential configuration logic (like custom seed data or third-party integrations) to test if the migration proceeds without hanging.
2. Clear Design-Time Cache
Cached build artifacts or design-time data can sometimes cause unexpected hangs. Try cleaning up and rebuilding:
- Delete the
objandbinfolders in both your startup project (GWMyBot) and data project (GWMyBot.Database). - Rebuild the entire solution, then re-run the migration command.
3. Implement an IDesignTimeDbContextFactory
For console apps, using an IDesignTimeDbContextFactory gives you explicit control over how EF Core creates your DbContext during migrations, bypassing the automatic service provider detection that might be causing issues. Here's a quick implementation you can add to your GWMyBot.Database project:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; namespace GWMyBot.Database { public class MyContextFactory : IDesignTimeDbContextFactory<MyContext> { public MyContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<MyContext>(); // Replace this with your actual connection string optionsBuilder.UseSqlServer("Your_Database_Connection_String"); return new MyContext(optionsBuilder.Options); } } }
Once this is added, re-run the migration command—EF Core will use this factory to create the DbContext directly, avoiding any service provider-related hang-ups.
4. Use the dotnet CLI Instead of Package Manager Console
Sometimes the Package Manager Console has hidden environment issues that the dotnet CLI doesn't. Try running the migration command directly from your solution directory:
dotnet ef migrations add owner --project GWMyBot.Database --startup-project GWMyBot --verbose
5. Verify Database Connectivity
Even though the DbContext is found, EF Core might be trying to connect to your database to compare schemas during migration generation. If there's a connection issue (like a firewall block, unresponsive database server, or incorrect connection string), it could hang.
- Double-check your connection string in
MyContextis correct. - Try connecting to the database using a tool like SSMS (for SQL Server) or pgAdmin (for PostgreSQL) to confirm it's reachable.
Command Execution Log (For Reference)
PM> Add-Migration owner -Verbose
Using project 'GWMyBot.Database'.
Using startup project 'GWMyBot'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Users\parab\Source\Repos\GWMyBot\GWMyBot\bin\Debug\netcoreapp2.2\GWMyBot.deps.json --additionalprobingpath C:\Users\parab.nuget\packages --additionalprobingpath C:\ProgramData\Xamarin\NuGet --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Users\parab\Source\Repos\GWMyBot\GWMyBot\bin\Debug\netcoreapp2.2\GWMyBot.runtimeconfig.json C:\Users\parab.nuget\packages\microsoft.entityframeworkcore.tools\3.1.3\tools\netcoreapp2.0\any\ef.dll migrations add owner --json --verbose --no-color --prefix-output --assembly C:\Users\parab\Source\Repos\GWMyBot\GWMyBot\bin\Debug\netcoreapp2.2\GWMyBot.Database.dll --startup-assembly C:\Users\parab\Source\Repos\GWMyBot\GWMyBot\bin\Debug\netcoreapp2.2\GWMyBot.dll --project-dir C:\Users\parab\Source\Repos\GWMyBot\GWMyBot.Database\ --language C# --working-dir C:\Users\parab\Source\Repos\GWMyBot --root-namespace GWMyBot.Database
Using assembly 'GWMyBot.Database'.
Using startup assembly 'GWMyBot'.
Using application base 'C:\Users\parab\Source\Repos\GWMyBot\GWMyBot\bin\Debug\netcoreapp2.2'.
Using working directory 'C:\Users\parab\Source\Repos\GWMyBot\GWMyBot'.
Using root namespace 'GWMyBot.Database'.
Using project directory 'C:\Users\parab\Source\Repos\GWMyBot\GWMyBot.Database'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'MyContext'.
Using context 'MyContext'.
内容的提问来源于stack exchange,提问作者Para

