jeudi 3 juin 2021

how to get Complex object with EFcore using foreign key?

I am using asp net core 5 for developing web API! I have a problem that i create my database with EFCORE and I want to query an object that has foreign key!

here is my code:

    public long RefreshTokenId { get; set; }
    public string JwtTokenId { get; set;}
    public User user { get; set; }
    public Int64 expirationTime { get; set; }
    public string Token { get; set; }
    public bool Revoked { get; set; }

in this class i use User Object as an object with foreign key in one to one relation according to efcore documentation!

here is my problem:

        public async Task<long> RefreshToken(RefreshRequest refreshRequest)
        {
        RefreshToken refreshToken = await Task.Run(() => {
            return _appContext.refreshTokens.SingleOrDefault(x => x.Token ==  
                 refreshRequest.refreshToken);
        });

         return refreshToken.user.UserId;
         }

when i run this function the null pointer exception is thrown! can anybody explain me how to retrive user object with efcore?




Aucun commentaire:

Enregistrer un commentaire