使用池时重置 DbContext 时调用什么?
What is called when a DbContext is reset when pooling is used?
DbContext
中是否有某种方法或挂钩可以知道何时通过上下文池重置它?我想缓存一些状态并将其清除。 release notes 表示:
Avoid using DbContext Pooling if you maintain your own state (for example, private fields) in your derived DbContext class that should not be shared across requests. EF Core will only reset the state that is aware of before adding a DbContext instance to the pool.
如何让 EF Core 识别数据?
好的,在深入了解 Entity Framework 代码后,这就是魔术酱。
- 创建一个 class 实现
IResettableService
- 我用锁定做了一些魔术来确保它的状态保持一致。
注册 IServiceCollection
:
services.AddScoped<ServiceContract, ServiceImplementation>()
.AddScoped<IResettableService>(sp => sp.GetRequiredService<ServiceContract>());
DbContext
中是否有某种方法或挂钩可以知道何时通过上下文池重置它?我想缓存一些状态并将其清除。 release notes 表示:
Avoid using DbContext Pooling if you maintain your own state (for example, private fields) in your derived DbContext class that should not be shared across requests. EF Core will only reset the state that is aware of before adding a DbContext instance to the pool.
如何让 EF Core 识别数据?
好的,在深入了解 Entity Framework 代码后,这就是魔术酱。
- 创建一个 class 实现
IResettableService
- 我用锁定做了一些魔术来确保它的状态保持一致。
注册
IServiceCollection
:services.AddScoped<ServiceContract, ServiceImplementation>() .AddScoped<IResettableService>(sp => sp.GetRequiredService<ServiceContract>());