System.InvalidOperationException ASP.NET C#

993 Views Asked by At

Solved Solved Solved Error; System.InvalidOperationException: 'An exception was thrown while attempting to evaluate a LINQ query parameter expression. See the inner exception for more information. To show additional information call 'DbContextOptionsBuilder.EnableSensitiveDataLogging'.'

When Is It Happening; When I try to use register method with those properties, Its poping up on return claims.ToList();:

    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string? MiddleName { get; set; }
    public string LastName { get; set; }

My Controller Method

[HttpPost("register")]
public IActionResult Register(RegisterDTO registerDTO)
{
var result = _authService.Register(registerDTO, registerDTO.Password);
if (!result.Success) return BadRequest(result);
var createAccessTokenResult = _authService.CreateAccessToken(result.Data);
if (!result.Success) return BadRequest(result);
    
var createdAccessTokenDataWithSuccessResult = new SuccessDataResult<AccessToken>(createAccessTokenResult.Data, result.Message);
    
return Ok(createdAccessTokenDataWithSuccessResult);
}

DataAccess Layer

public List<OperationClaim> GetClaims(User user)
        {
            var claims = from uoc in Context.UserOperationClaims
                         join oc in Context.OperationClaims
                         on uoc.OperationClaimId equals oc.Id
                         where uoc.UserId == user.Id
                         select new OperationClaim
                         { Id = oc.Id, Name = oc.Name };

            return claims.ToList();
        }

Entity User;

public int Id { get; set; }
    public string FirstName { get; set; }
    public string? MiddleName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public byte[] PasswordSalt { get; set; }
    public byte[] PasswordHash { get; set; }
    public bool Status { get; set; }

I solved it The problem is, Im was not sending parameter user when It is success on Business Layer

public IDataResult<User> Register(RegisterDTO registerDTO, string password)
        {
            BusinessRules.Run(CheckTheEmailIsAlreadyRegistered(registerDTO));

            byte[] passwordHash, passwordSalt;
            HashingHelper.CreatePasswordPash(password, out passwordHash, out passwordSalt);

            var user = new User()
            {
                Email = registerDTO.Email,
                FirstName = registerDTO.FirstName,
                MiddleName = registerDTO.MiddleName,
                LastName = registerDTO.LastName,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,
                Status = true
            };

            var result = _userService.Add(user);

            if (!result.Success) return new ErrorDataResult<User>();

            return new SuccessDataResult<User>(**user**);
        }
0

There are 0 best solutions below