You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a jsonb column that stores an object with enum property. I configured all enums to serialize as strings. However even though It is written as string it is still read as number that results in error.
Here is a console app that reproduces the problem:
using System.Text.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using Npgsql;
using var db = new MyContext();
db.Database.EnsureCreated();
var user = new User()
{
Details = new()
{
Address = "some address",
UserType = UserType.Admin
}
};
db.Users.Add(user);
db.SaveChanges(); // saved successfully {"Address": "some address", "UserType": "Admin"}
user = db.Users.FirstOrDefault(); // read whole user successfully
var userType = db.Users.Select(x => x.Details.UserType).FirstOrDefault(); // fails 22P02: invalid input syntax for type integer: "Admin"
Console.WriteLine(userType);
class MyContext : DbContext
{
public DbSet<User> Users { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dataSource = new NpgsqlDataSourceBuilder("Host=localhost;Port=5432;Database=any;Username=any;Password=any")
.EnableDynamicJson()
.ConfigureJsonOptions(new()
{
PropertyNameCaseInsensitive = true,
Converters =
{
new JsonStringEnumConverter(allowIntegerValues: true),
},
})
.Build();
optionsBuilder.UseNpgsql(dataSource);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(o =>
{
o.Property(p => p.Details).HasColumnType("jsonb");
});
}
}
record User
{
public long Id { get; set; }
public UserDetails Details { get; set; }
}
record UserDetails
{
public string Address { get; set; }
public UserType UserType { get; set; }
}
enum UserType
{
Manager = 1,
Admin = 2
}
The text was updated successfully, but these errors were encountered:
I'm having a problem similar to #2325
I have a jsonb column that stores an object with enum property. I configured all enums to serialize as strings. However even though It is written as string it is still read as number that results in error.
Here is a console app that reproduces the problem:
The text was updated successfully, but these errors were encountered: