gpt4 book ai didi

c# - 如何调试 dotnet ef 数据库删除?

转载 作者:行者123 更新时间:2023-12-05 08:54:56 27 4
gpt4 key购买 nike

在运行 dotnet ef database drop 时出现错误。我如何调试此命令以获取有关该问题的更多详细信息?或者我怎样才能找到这个错误的根源?

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](IInfrastructure1 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".

最佳答案

我有同样的问题并将其添加到 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}");
}

并在 DbContext 的构造函数和 Dispose 中调用此函数。

这似乎是一个问题,如果 DbContext 被添加到依赖注入(inject)中,并带有作用域生命周期。我为此提出了一个问题。

https://github.com/aspnet/EntityFrameworkCore/issues/10693

关于c# - 如何调试 dotnet ef 数据库删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47994546/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com