发布:2023/12/7 15:25:27作者:大数据 来源:大数据 浏览次数:469
asp.net core在ConfigureServices如何读取数据和获取Dbcontext
startup.cs 的 ConfigureServices 这里确实可以连接数据库,这个方法很关键,Get
https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.serviceproviderserviceextensions?view=aspnetcore-2.0
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//带scope using (var iServiceScope = services.BuildServiceProvider().CreateScope()) { using (var dbcontext = iServiceScope.ServiceProvider.GetRequiredService<EventReportDbContext>()) { //... } } //不带Scope using (var dbcontext = services.BuildServiceProvider().GetRequiredService<EventReportDbContext>()) { //... } |
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4