有什么方法或钩子 DbContext 要知道何时通过上下文池重置它?我想缓存一些状态并清除它。这个 release notes 请说明:
DbContext
如果在派生的dbcontext类中维护自己的状态(例如,私有字段),而这些状态不应在请求之间共享,请避免使用dbcontext池。ef core将只重置在将dbcontext实例添加到池之前知道的状态。
如何让ef core知道数据?
好吧,在深入研究实体框架代码之后,这里是神奇的调味品。
IResettableService
向注册 IServiceCollection :
IServiceCollection
services.AddScoped<ServiceContract, ServiceImplementation>() .AddScoped<IResettableService>(sp => sp.GetRequiredService<ServiceContract>());