EF: Switch to Find where possible

This commit is contained in:
Hao Kung 2016-10-03 13:44:39 -07:00
parent d18ae5cad1
commit 971292544a
2 changed files with 16 additions and 20 deletions

View File

@ -239,6 +239,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
/// </summary> /// </summary>
public IdentityErrorDescriber ErrorDescriber { get; set; } public IdentityErrorDescriber ErrorDescriber { get; set; }
private DbSet<TUser> UsersSet { get { return Context.Set<TUser>(); } }
private DbSet<TRole> Roles { get { return Context.Set<TRole>(); } } private DbSet<TRole> Roles { get { return Context.Set<TRole>(); } }
private DbSet<TUserClaim> UserClaims { get { return Context.Set<TUserClaim>(); } } private DbSet<TUserClaim> UserClaims { get { return Context.Set<TUserClaim>(); } }
private DbSet<TUserRole> UserRoles { get { return Context.Set<TUserRole>(); } } private DbSet<TUserRole> UserRoles { get { return Context.Set<TUserRole>(); } }
@ -472,7 +473,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed(); ThrowIfDisposed();
var id = ConvertIdFromString(userId); var id = ConvertIdFromString(userId);
return Users.FirstOrDefaultAsync(u => u.Id.Equals(id), cancellationToken); return UsersSet.FindAsync(new object[] { id }, cancellationToken);
} }
/// <summary> /// <summary>
@ -496,7 +497,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
/// <returns>An <see cref="string"/> representation of the provided <paramref name="id"/>.</returns> /// <returns>An <see cref="string"/> representation of the provided <paramref name="id"/>.</returns>
public virtual string ConvertIdToString(TKey id) public virtual string ConvertIdToString(TKey id)
{ {
if (Object.Equals(id, default(TKey))) if (object.Equals(id, default(TKey)))
{ {
return null; return null;
} }
@ -523,7 +524,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
/// </summary> /// </summary>
public virtual IQueryable<TUser> Users public virtual IQueryable<TUser> Users
{ {
get { return Context.Set<TUser>(); } get { return UsersSet; }
} }
/// <summary> /// <summary>
@ -624,7 +625,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
var roleEntity = await Roles.SingleOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken); var roleEntity = await Roles.SingleOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken);
if (roleEntity != null) if (roleEntity != null)
{ {
var userRole = await UserRoles.FirstOrDefaultAsync(r => roleEntity.Id.Equals(r.RoleId) && r.UserId.Equals(user.Id), cancellationToken); var userRole = await UserRoles.FindAsync(new object[] { user.Id, roleEntity.Id }, cancellationToken);
if (userRole != null) if (userRole != null)
{ {
UserRoles.Remove(userRole); UserRoles.Remove(userRole);
@ -677,9 +678,8 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
var role = await Roles.SingleOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken); var role = await Roles.SingleOrDefaultAsync(r => r.NormalizedName == normalizedRoleName, cancellationToken);
if (role != null) if (role != null)
{ {
var userId = user.Id; var userRole = await UserRoles.FindAsync(new object[] { user.Id, role.Id }, cancellationToken);
var roleId = role.Id; return userRole != null;
return await UserRoles.AnyAsync(ur => ur.RoleId.Equals(roleId) && ur.UserId.Equals(userId));
} }
return false; return false;
} }
@ -888,8 +888,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed(); ThrowIfDisposed();
var userLogin = await var userLogin = await UserLogins.FindAsync(new object[] { loginProvider, providerKey }, cancellationToken);
UserLogins.FirstOrDefaultAsync(l => l.LoginProvider == loginProvider && l.ProviderKey == providerKey, cancellationToken);
if (userLogin != null) if (userLogin != null)
{ {
return await Users.FirstOrDefaultAsync(u => u.Id.Equals(userLogin.UserId), cancellationToken); return await Users.FirstOrDefaultAsync(u => u.Id.Equals(userLogin.UserId), cancellationToken);
@ -1348,7 +1347,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
ThrowIfDisposed(); ThrowIfDisposed();
if (String.IsNullOrEmpty(normalizedRoleName)) if (string.IsNullOrEmpty(normalizedRoleName))
{ {
throw new ArgumentNullException(nameof(normalizedRoleName)); throw new ArgumentNullException(nameof(normalizedRoleName));
} }
@ -1368,10 +1367,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
} }
private Task<TUserToken> FindToken(TUser user, string loginProvider, string name, CancellationToken cancellationToken) private Task<TUserToken> FindToken(TUser user, string loginProvider, string name, CancellationToken cancellationToken)
{ => UserTokens.FindAsync(new object[] { user.Id, loginProvider, name }, cancellationToken);
var userId = user.Id;
return UserTokens.SingleOrDefaultAsync(l => l.UserId.Equals(userId) && l.LoginProvider == loginProvider && l.Name == name, cancellationToken);
}
/// <summary> /// <summary>
/// Sets the token value for a particular user. /// Sets the token value for a particular user.
@ -1420,8 +1416,7 @@ namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore
{ {
throw new ArgumentNullException(nameof(user)); throw new ArgumentNullException(nameof(user));
} }
var userId = user.Id; var entry = await FindToken(user, loginProvider, name, cancellationToken);
var entry = await UserTokens.SingleOrDefaultAsync(l => l.UserId.Equals(userId) && l.LoginProvider == loginProvider && l.Name == name, cancellationToken);
if (entry != null) if (entry != null)
{ {
UserTokens.Remove(entry); UserTokens.Remove(entry);

View File

@ -1534,16 +1534,17 @@ namespace Microsoft.AspNetCore.Identity.Test
IdentityResultAssert.IsSuccess(await userManager.CreateAsync(u)); IdentityResultAssert.IsSuccess(await userManager.CreateAsync(u));
} }
var r = CreateTestRole("r1"); var r = CreateTestRole("r1");
var roleName = await roleManager.GetRoleNameAsync(r);
IdentityResultAssert.IsSuccess(await roleManager.CreateAsync(r)); IdentityResultAssert.IsSuccess(await roleManager.CreateAsync(r));
foreach (var u in users) foreach (var u in users)
{ {
IdentityResultAssert.IsSuccess(await userManager.AddToRoleAsync(u, await roleManager.GetRoleNameAsync(r))); IdentityResultAssert.IsSuccess(await userManager.AddToRoleAsync(u, roleName));
Assert.True(await userManager.IsInRoleAsync(u, await roleManager.GetRoleNameAsync(r))); Assert.True(await userManager.IsInRoleAsync(u, roleName));
} }
foreach (var u in users) foreach (var u in users)
{ {
IdentityResultAssert.IsSuccess(await userManager.RemoveFromRoleAsync(u, await roleManager.GetRoleNameAsync(r))); IdentityResultAssert.IsSuccess(await userManager.RemoveFromRoleAsync(u, roleName));
Assert.False(await userManager.IsInRoleAsync(u, await roleManager.GetRoleNameAsync(r))); Assert.False(await userManager.IsInRoleAsync(u, roleName));
} }
} }