There is the following database context:
public class AppIdentityContext : IdentityDbContext<UserAccount, UserRole, int> { public AppIdentityContext(DbContextOptions<AppIdentityContext> options) : base(options) { } public DbSet<RefreshToken> RefreshTokens { get; set; } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<RefreshToken>() .HasKey(rt => rt.Refresh); builder.Entity<RefreshToken>() .HasOne(rt => rt.UserAccount) .WithMany(ua => ua.RefreshTokens) .HasForeignKey(rt => rt.UserId); } } The entity classes themselves:
public class UserAccount : IdentityUser<int> { // other properties public virtual ICollection<RefreshToken> RefreshTokens { get; set; } } public class RefreshToken { public string Refresh { get; set; } // other properties public int UserId { get; set; } public virtual UserAccount UserAccount { get; set; } } Next, I get the RefreshToken in the method:
public async Task<RefreshToken> GetByRefreshAsync(string token) { return await _context.RefreshTokens.FirstOrDefaultAsync(t => t.Refresh == token); } //... var currentToken = await _tokenRep.GetByRefreshAsync(refreshToken); if (currentToken.UserAccount == null) { //... } //... So, it seems that it ignores the connection established earlier in DbContext, because UserAccount always null .
Judging by the logs, it does not request the UserAccount either at the time of receiving RefreshToken , or at the time of accessing the UserAccount field.
If you explicitly request a user, then everything works fine.
var userAccount = await _userManager.FindByIdAsync(currentToken.UserId.ToString()); if (userAccount == null) { //... } //...