Home > Net >  Can't cast database type xid8 to Int64
Can't cast database type xid8 to Int64

Time:01-18

Is there a way to enable the mapping conversion in EF from internal Postgres type xi8 to any .NET runtime type like int, uint, long, ulong or string ?

I was trying without any success with the following mapping:

public class History
{
    public Guid Id { get; }
    public uint VersionId { get; set; }
    public DateTimeOffset Date { get; set; }
    public string Status { get; set; }
}

Database context configuration:

public class PostgresDbContext : DbContext
{
    public PostgresDbContext(DbContextOptions options)
        : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<History>()
            .HasKey(x => x.Id);

        modelBuilder.Entity<History>()
            .Property(x => x.Date);

        modelBuilder.Entity<History>()
            .Property(x => x.Status);

        modelBuilder.Entity<History>()
            .Property(x => x.VersionId)
            .HasColumnName("txid");

        modelBuilder.Entity<History>()
            .ToTable("history");
    }
}

Table creation script:

CREATE TABLE IF NOT EXISTS public.history
(
    id uuid NOT NULL,
    date timestamp with time zone NOT NULL DEFAULT now(),
    status text COLLATE pg_catalog."default" NOT NULL,
    txid xid8 NOT NULL DEFAULT pg_current_xact_id(),
    CONSTRAINT history_pkey PRIMARY KEY (id)
)

Seed:

INSERT INTO public.history (id, date, status, txid) VALUES ('76a2703b-dfbd-478f-9c73-9ec1aa9fc9f2', '2023-01-15 12:06:25.190194 00', 'deleted', '778');
INSERT INTO public.history (id, date, status, txid) VALUES ('9922ee0c-2815-4e92-9d3a-083e83fcc518', '2023-01-15 16:50:46.284947 00', 'updated', '779');
INSERT INTO public.history (id, date, status, txid) VALUES ('464c3fcb-7bfc-4026-87fe-6427a21b967f', '2023-01-15 21:05:31.29307 00', 'updated', '780');

Exception message:

Can't cast database type xid8 to Int64
   at Npgsql.Internal.TypeHandling.NpgsqlTypeHandler.ReadCustom[TAny](NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription) 
   at Npgsql.NpgsqlDataReader.GetFieldValue[T](Int32 ordinal)    
   at Npgsql.NpgsqlDataReader.GetInt64(Int32 ordinal)  

Target framework: .NET 7

Nuget packages:

  • Npgsql.EntityFrameworkCore.PostgreSQL, Version="7.0.1"
  • Microsoft.EntityFrameworkCore, Version="7.0.2"

CodePudding user response:

Cross-posted as a github issue: https://github.com/npgsql/efcore.pg/issues/2618.

tl;dr xid8 isn't currently supported but xid is (and xid8 is convertible to it). I've merged support for xid8 for version 8 of the provider.

  • Related