Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties mapping to PostgreSQL Json/JsonB types is not working. #393

Open
GlebGQ opened this issue Jul 4, 2024 · 0 comments
Open

Properties mapping to PostgreSQL Json/JsonB types is not working. #393

GlebGQ opened this issue Jul 4, 2024 · 0 comments

Comments

@GlebGQ
Copy link

GlebGQ commented Jul 4, 2024

I have the following entity:

 public class Message
  {
    public Guid Id { get; set; }
    public string Payload { get; set; }
    public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>();
  }

with the EF configuration:

public class MessageConfiguration : IEntityTypeConfiguration<Message>
{
    private readonly string _schema;
    private readonly string _table;

    public OutboxEntityConfiguration(string schema, string table)
    {
        _schema = schema;
        _table = table;
    }

    public void Configure(EntityTypeBuilder<Message> builder)
    {
        _ = builder.ToTable(_table, _schema);

        _ = builder.HasKey(x => x.Id);

        _ = builder.Property(x => x.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        _ = builder.Property(x => x.Payload)
            .HasColumnName("payload")
            .HasColumnType("jsonb");

        _ = builder.Property(x => x.Headers)
            .HasColumnType("json")
            .HasColumnName("headers");
    }
}

but when I'm trying to execute the following code:

await _dbContext.Messages
            .ToLinqToDBTable()
            .Merge()
            .Using(entities)
            .OnTargetKey()
            .InsertWhenNotMatched()
            .MergeAsync(cancellationToken);

The exception is thrown:

Error
Exception: Npgsql.PostgresException
Message  : 42804: column "payload" is of type jsonb but expression is of type text

The generated SQL is the following:

DECLARE @value Json -- Object
SET     @value = {[traceparent, 00-d82e97dfd4a6f2f9b5439e35f3300fa2-55f22fb896172514-01]}

MERGE INTO messages "Target"
USING (VALUES ('2edcf165-b97a-4026-89a6-95cc6132532d'::uuid,,'{"jsonProp1":"8421ac39-830e-4251-81d5-619741045408","jsonProp2":0,"jsonProp3":"value3"}',:value) 
) "Source"
(
        "Id",
        "Payload",
        "Headers"
)
ON ("Target".id = "Source"."Id")

WHEN NOT MATCHED THEN
INSERT
(
        id,
        payload,
        headers,
)
VALUES
(
        "Source"."Id",
        "Source"."Payload",
        "Source"."Headers",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant