tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 586 forks source link

Update does not work with guid primary key in PostgreSQL #280

Closed yoyrandao closed 2 years ago

yoyrandao commented 2 years ago

I'm trying to perform Update method, but it fails with execution error:

Npgsql.PostgresException : 42883: operator does not exist: character varying = uuid
Data:
  Severity: ERROR
  InvariantSeverity: ERROR
  SqlState: 42883
  MessageText: operator does not exist: character varying = uuid
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
  Position: 104
  File: parse_oper.c
  Line: 647
  Routine: op_error

Model:

public record Account : Entity
{
      public string? Name { get; init; }

      public string? TokenHash { get; set; }

      public string? CertificateThumbprint { get; init; }

      public Guid[]? RoleIds { get; init; }
}

public record Entity
{
      public Guid Id { get; set; }
}

Mapping:

internal sealed class AccountMapper : ClassMapper<Account>
{
    public AccountMapper()
    {
        Table("account");

        Map(x => x.Id).Column(nameof(Account.Id).ToLower()).Type(DbType.Guid).Key(KeyType.Guid);

        Map(x => x.Name).Column(nameof(Account.Name).ToLower()).Type(DbType.String);
        Map(x => x.TokenHash).Column(nameof(Account.TokenHash).ToLower()).Type(DbType.String);
        Map(x => x.CertificateThumbprint).Column(nameof(Account.CertificateThumbprint).ToLower()).Type(DbType.String);

        Map(x => x.RoleIds).Ignore();

        AutoMap();
    }
}

Update method:

public Account? Update(Account account)
{
    using var connection = _connectionFactory.Create(ConnectionString);

    connection.Open();
    connection.Update(account);

    return account;
}

And table:

CREATE TABLE public.account (
    id                    UUID  NOT NULL PRIMARY KEY,
    name              VARCHAR(255) NULL,
    tokenHash             VARCHAR(64)  NULL,
    certificateThumbprint VARCHAR(40)  NULL
);

Am i doing something wrong? Please help

yoyrandao commented 2 years ago

Okay, i solved this with setting KeyType.Assigned instead of KeyType.Guid