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

Mapping enums. #3415

Closed
ShelestSergey opened this issue Dec 20, 2024 · 1 comment
Closed

Mapping enums. #3415

ShelestSergey opened this issue Dec 20, 2024 · 1 comment

Comments

@ShelestSergey
Copy link

ShelestSergey commented Dec 20, 2024

I think that I'm doing something wrong but I can't realize what.
I have scaffolded DB with a bunch of enums and one composite type.
In Startup.cs I have:

var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
dataSourceBuilder.EnableUnmappedTypes();
dataSourceBuilder.MapComposite<Dimension>("main.dimension");
var dataSource = dataSourceBuilder.Build();
services.AddDbContext<KsDbContext>(options =>
{
    options.UseNpgsql(dataSource,
    o =>
    {
        o.MapEnum<AssignmentState>("assignmentstate", "operation", new NpgsqlNullNameTranslator());
        o.MapEnum<Rowstate>("rowstate", "main", new NpgsqlNullNameTranslator());
...

In DBContext.cs:

entity.Property(e => e.Status).HasColumnName("status");
entity.Property(e => e.State).HasColumnName("state");

When I remove EnableUnmappedTypes, I receive an error:
Reading as 'MesOps.AppServer.Enums.Rowstate' is not supported for fields having DataTypeName 'main.rowstate' - System.NotSupportedException: Reading and writing unmapped enums requires an explicit opt-in; call 'EnableUnmappedTypes' on 'NpgsqlDataSourceBuilder' or NpgsqlConnection.GlobalTypeMapper

I think, that I'm mapping columns incorrect. But I can't understand what exactly...

NpgSql - v. 9.0.2
.NET 9.0

@roji
Copy link
Member

roji commented Dec 21, 2024

You shouldn't be using EnableUnmappedTypes() for enums when using EF - that option is needed only when using Npgsql without EF, and wanting to not pre-map your enums in advance (e.g. for various dynamic scenarios).

Give the enum mapping docs a good read - as you'er using EF 9, I'd recommend simply using the new ConfigureDataSource() API, which is the easiest way to get things running.

Note that the EF provider doesn't support composite types (#22 tracks that).

If you're still having trouble after following the documentation, please post a minimal, runnable repro. In the meantime, I'll go ahead and close this as a usage issue, but feel free to post back here if you're still encountering issues.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants