Accessing Navigation Properties from IdentityUser when LazyLoading is off

后端 未结 2 1931
暗喜
暗喜 2021-01-13 06:43

I\'ve this setup with code first model:

public class TestContext :IdentityDbContext
{
    public TestContext()
        : base(\"TestConnectio         


        
2条回答
  •  粉色の甜心
    2021-01-13 07:25

    I have solved with this:

    Create a custom UserManager

    public class ApplicationUserManager : UserManager
    {
        public ApplicationUserManager(IUserStore store, IOptions optionsAccessor, IPasswordHasher passwordHasher, IEnumerable> userValidators, IEnumerable> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger> logger, IHttpContextAccessor contextAccessor) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger, contextAccessor) { }
    
        public override Task FindByIdAsync(string userId)
        {
            return Users.Include(c => c.Esercizio).FirstOrDefaultAsync(u => u.Id == userId);
        }
    }
    

    Replace default UserManager service

    In your ConfigureServices add this:

    services.AddIdentity().AddEntityFrameworkStores().AddDefaultTokenProviders().AddUserManager();
    

    Change arguments for DI

    from

    [FromServices]UserManager userManager
    

    to

    [FromServices]ApplicationUserManager userManager
    

    I hope this helps

提交回复
热议问题