-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DbContext的生命周期问题 #14
Comments
至于使用了Scoped方式后,EntityFrameworkCore.Triggers中报错问题, Triggers<SysMenu>.Updating += entry =>
{
var parentMenu = entry.Context.Set<SysMenu>().Find(entry.Entity.ParentId);
entry.Entity.SortIndex = entry.Entity.Id;
entry.Entity.MenuPath = (parentMenu?.MenuPath ?? "0") + "," + entry.Entity.Id;
}; |
谢谢指正。在项目里,DbContext和Repository的生命周期都由DI控制。DbContext的生命周期我是参照AddDbContext来的,源码里是用的AddSingleton。我刚刚测试了一下,改为Scoped后会报错,提示无法访问已释放过的资源。改为Singleton和Transient都没问题。 |
我已经测试过了,此问题是由Triggers引起,解决方式也在以上列出。 entry.Context.Set<SysMenu>().Find(entry.Entity.ParentId); |
我已经修改了和更新了nuget,并将demo分离出来新建了一个repository。万分感谢指正~~ @LambertW |
Dev分支中,使用的是AddSingleton()方式,不合适。
应该使用AddScoped进行注入。
The text was updated successfully, but these errors were encountered: