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) { //... } //... 

    1 answer 1

    EF to 2.1 does not support lazy loading, even if you have just 2.1, you need to separately enable it.

    Turning on you need to check that the conditions for lazy loading are fulfilled (public classes, virtual properties).

    Well, traditionally I recommend not to include lazy loading, to indicate explicitly in each case what you need for the fields. Load them explicitly:

     await _context.RefreshTokens.Include(x => x.UserAccount ) .FirstOrDefaultAsync(t => t.Refresh == token); 

    See also: