Skip to content
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

Closed
LambertW opened this issue Apr 29, 2018 · 4 comments
Closed

DbContext的生命周期问题 #14

LambertW opened this issue Apr 29, 2018 · 4 comments

Comments

@LambertW
Copy link

Dev分支中,使用的是AddSingleton()方式,不合适。
应该使用AddScoped
进行注入。

@LambertW
Copy link
Author

LambertW commented Apr 29, 2018

至于使用了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;
            };

@VictorTzeng
Copy link
Owner

谢谢指正。在项目里,DbContext和Repository的生命周期都由DI控制。DbContext的生命周期我是参照AddDbContext来的,源码里是用的AddSingleton。我刚刚测试了一下,改为Scoped后会报错,提示无法访问已释放过的资源。改为Singleton和Transient都没问题。

@LambertW
Copy link
Author

谢谢指正。在项目里,DbContext和Repository的生命周期都由DI控制。DbContext的生命周期我是参照AddDbContext来的,源码里是用的AddSingleton。我刚刚测试了一下,改为Scoped后会报错,提示无法访问已释放过的资源。改为Singleton和Transient都没问题。

我已经测试过了,此问题是由Triggers引起,解决方式也在以上列出。

entry.Context.Set<SysMenu>().Find(entry.Entity.ParentId);

@VictorTzeng
Copy link
Owner

我已经修改了和更新了nuget,并将demo分离出来新建了一个repository。万分感谢指正~~ @LambertW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants