[英]ASP.NET Core Identity. Using ApplicationDbContext and UserManager. Do they share the context?
i have an database initialization method in an ASP.NET MVC Core 2 application. 我在ASP.NET MVC Core 2应用程序中有一个数据库初始化方法。 In that method i'm using ApplicationDbContext and UserManager to initialize the database. 在该方法中,我使用ApplicationDbContext和UserManager来初始化数据库。 I get both instances from the constructor's dependency injection: 我从构造函数的依赖注入中获取了两个实例:
public static void Initialize(ApplicationDbContext context,
UserManager<ApplicationUser> user)
What i want to know is if they share the same context instance or if one context is created for ApplicationDbContext and another for UserManager. 我想知道的是,如果它们共享相同的上下文实例,或者为ApplicationDbContext创建了一个上下文,而为UserManager创建了另一个上下文。 If i execute something like this: 如果我执行这样的事情:
ApplicationUser adminTeacherUser = new ApplicationUser
{
UserName = "test@test.com",
Email = "test@test.com",
EmailConfirmed = true,
Name = "test",
LastName = "test",
BirthDate = null,
EntryDate = DateTime.Now
};
userManager.CreateAsync(adminTeacherUser, "password").Wait();
The user is created at database inmediatly after the CreateAsync call. 在CreateAsync调用之后,用户在数据库中创建。
But, if then i update the user like this: 但是,如果那时我像这样更新用户:
adminTeacherUser.Name = "other";
userManager.UpdateAsync(adminTeacherUser);
After the call to UpdateAsync nothing is updated at the database. 调用UpdateAsync后,数据库中不会更新任何内容。 The name of the user stills being "test". 用户名仍然是“测试”。
But, if then i execute: 但是,如果那时我执行:
context.SaveChanges();
The changes are applied and the database is updated. 将应用更改并更新数据库。
So, why the CreateAsync method "saves its changes" without explicitly calling "context.SaveChanges" and the UpdateAsync method needs it? 那么,为什么CreateAsync方法“保存其更改”而不显式调用“context.SaveChanges”并且UpdateAsync方法需要它? Is the ApplicationDbContext instance i get by dependency injection the same that CreateAsync is using? ApplicationDbContext实例是通过依赖注入得到的,与CreateAsync使用的相同吗?
Thank you. 谢谢。
The default registration of Entity Framework Core in an ASP.NET Core application is a per-request scope, so they will definitively share the context. ASP.NET Core应用程序中的Entity Framework Core的默认注册是按请求范围,因此它们将明确地共享上下文。
As you can see from the source code of the default implementation of EF Core for ASP.NET Core Identity ( here 正如您可以从EF Core for ASP.NET Core Identity的默认实现的源代码中看到的( 这里
/// <summary>
/// Creates the specified <paramref name="user"/> in the user store.
/// </summary>
/// <param name="user">The user to create.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> of the creation operation.</returns>
public async override Task<IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
Context.Add(user);
await SaveChanges(cancellationToken);
return IdentityResult.Success;
}
/// <summary>
/// Updates the specified <paramref name="user"/> in the user store.
/// </summary>
/// <param name="user">The user to update.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> of the update operation.</returns>
public async override Task<IdentityResult> UpdateAsync(TUser user, CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed();
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
Context.Attach(user);
user.ConcurrencyStamp = Guid.NewGuid().ToString();
Context.Update(user);
try
{
await SaveChanges(cancellationToken);
}
catch (DbUpdateConcurrencyException)
{
return IdentityResult.Failed(ErrorDescriber.ConcurrencyFailure());
}
return IdentityResult.Success;
}
, they both save changes. ,他们都保存了变化。
The problem you are experiencing is that you are not await
ing for the result of UpdateAsync
, so you need to change to: 您遇到的问题是您没有await
UpdateAsync
的结果,因此您需要更改为:
await userManager.UpdateAsync(adminTeacherUser);
//or
userManager.UpdateAsync(adminTeacherUser).Wait();
Of course, using async-all-the-way is the preferred version and you shouldn't block using .Wait()
. 当然,使用async-all-the-way是首选版本,您不应该阻止使用.Wait()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.