On Running dotnet ef database drop
I get an error. How can I debug this command, to get more details about the problem? Or how can I find the source of this error?
System.ObjectDisposedException: Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Objektname: "ApplicationDbContext". bei Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() bei Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.get_Instance() bei Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure
1 accessor) bei Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
1 factory) bei Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) bei Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.DropDatabase(String contextType) bei Microsoft.EntityFrameworkCore.Design.OperationExecutor.DropDatabaseImpl(String contextType) bei Microsoft.EntityFrameworkCore.Design.OperationExecutor.DropDatabase.<>c__DisplayClass0_1.<.ctor>b__0() bei Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Objektname: "ApplicationDbContext".
I have the same problem and added this to the DbContext:
private void PrintStackTrace()
{
var st = new StackTrace();
foreach (var sf in st.GetFrames())
Console.WriteLine($"{sf.GetMethod().DeclaringType.Assembly.GetName().Name} {sf.GetMethod().DeclaringType.Name} {sf.GetMethod().Name}");
}
and call this function in the contructor and Dispose of the DbContext.
It seems to be a problem, if the DbContext is added with scoped lifetime to the dependency injection. I filed an issue for that.
add the code to wait for a debugger in your DbContext constructor and you'll be able to debug :
public class ApplicationDbContext: DbContext
{
public ApplicationDbContext()
{
while (!Debugger.IsAttached)
{
Thread.Sleep(100);
}
}
...
}
Then attach the dotnet process using your favorite debugger.