How to load navigation properties on an IdentityUser with UserManager

前端 未结 3 1731
离开以前
离开以前 2020-12-03 05:01

I\'ve extended IdentityUser to include a navigation property for the user\'s address, however when getting the user with UserManager.FindByEmailAsync

3条回答
  •  孤城傲影
    2020-12-03 05:17

    Unfortunately, you have to either do it manually or create your own IUserStore where you load related data in the FindByEmailAsync method:

    public class MyStore : IUserStore, // the rest of the interfaces
    {
        // ... implement the dozens of methods
        public async Task FindByEmailAsync(string normalizedEmail, CancellationToken token)
        {
            return await context.Users
                .Include(x => x.Address)
                .SingleAsync(x => x.Email == normalizedEmail);
        }
    }
    

    Of course, implementing the entire store just for this isn't the best option.

    You can also query the store directly, though:

    UserManager userManager; // DI injected
    
    var user = await userManager.Users
        .Include(x => x.Address)
        .SingleAsync(x => x.NormalizedEmail == email);
    

提交回复
热议问题