问题描述
自定义数据库初始化类中出现以下异常:
I'm getting the following exception in my custom database initializer class:
这是初始化器:
public class DbInitializer : IDbInitializer
{
private readonly ApplicationDbContext _Context;
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<ApplicationRole> _roleManager;
public DbInitializer(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
RoleManager<ApplicationRole> roleManager)
{
_context = context;
_userManager = userManager;
_roleManager = roleManager;
}
public async void Initialize()
{
var adminUsers = _userManager.GetUsersInRoleAsync(RoleClaimValues.Admin).Result;
if (adminUsers == null || adminUsers.Count == 0)
{
(...)
}
}
}
public interface IDbInitializer
{
void Initialize();
}
这是startup.cs,在其中我在ConfigureService
中添加IDbInitializer
的范围,然后从Configure
调用它:
And here is the startup.cs where i add the scope of IDbInitializer
in ConfigureService
and then call it from Configure
:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ApplicationConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>(o => {
o.Password.RequireDigit = false;
o.Password.RequiredLength = 4;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, MessageServices>();
//Seed database
services.AddScoped<IDbInitializer, DbInitializer>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDbInitializer dbInitializer)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
app.UseStatusCodePages("text/plain", "Status code page, status code: {0}");
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
dbInitializer.Initialize();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
我不知道为什么异常告诉我们问题可能出在上下文放置在某个地方.
I don't know why the exception tells that the problem could be that the context is disposed somewhere.
我还添加了
.UseDefaultServiceProvider(options =>
options.ValidateScopes = false)
在.UseStartup<Startup>()
之后,以防万一与范围有关,但例外是相同的.
after .UseStartup<Startup>()
just in case it was something relative to the scopes, but the exception is the same.
推荐答案
我遇到了与您相同的问题.您确实必须更改所有代码才能返回Task
.否则,DbContext
将在所有操作完成之前被处理掉.
I had the same problem as you had.You indeed have to change all code to return a Task
. Otherwise the DbContext
is disposed before all actions are done.
数据库初始化在dotnet core 2.0中进行了更改.我创建了有关此特定主题的博客.参见 http://www.locktar.nl/programming/net-core/seed-database-users-roles-dotnet-core-2-0-ef/了解更多信息.
Database Initialization is changed in dotnet core 2.0. I created a blog about this specific topic.See http://www.locktar.nl/programming/net-core/seed-database-users-roles-dotnet-core-2-0-ef/ for more information.
这篇关于ASP.NET Core 2.0 MVC自定义数据库初始化程序类上的ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!