CoolLife
CoolLife

Reputation: 1479

No operator matches the given name and argument type. You might need to add an explicit type cast

Cant run my query I have a "Id" on my db of "users" this is type "uuid" of postgres, I have a GUID id on dot net core and I want get thee user with the same id, and return this user.

    public async Task<T> GetById(Guid entity)
    {
        try
        {
            connection.Open();
            var result = await this.connection.QueryFirstAsync(askForId, entity);
            return result;
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            connection.Close();
        }
    }

Entity has a value of:

entity: {675d83ca-c7b6-4efe-bf98-46a48e8a28fd}

My query is this:

private readonly string askForId = "SELECT * FROM Users WHERE Id = @Id";

How can give a solution to this:

 "ClassName": "Npgsql.PostgresException",
    "Message": "External component has thrown an exception.",
    "Data": {
        "Severity": "ERROR",
        "InvariantSeverity": "ERROR",
        "SqlState": "42883",
        "MessageText": "operator does not exist: @ uuid",
        "Hint": "No operator matches the given name and argument type. You might need to add an explicit type cast.",
        "Position": 32,
        "File": "parse_oper.c",
        "Line": "731",
        "Routine": "op_error"
    },
}

Upvotes: 0

Views: 6146

Answers (1)

CoolLife
CoolLife

Reputation: 1479

I change my query just the @Id to @id:

private readonly string askForId = "SELECT * FROM Users WHERE Id = @id";

and change to QueryFirstAsync to QueryFirstOrDefaultAsync becuase I need know if dont find nothing.

public async Task<T> GetById(Guid entity)
        {
            try
            {
                connection.Open();
                var result = await this.connection.QueryFirstOrDefaultAsync<T>(askForId, new { id = entity});
                return result;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                connection.Close();
            }
        }

And change the entity to new { id = entity} and work fine the query

Upvotes: 1

Related Questions