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

Enum resets to default value on insert. It can't differentiate between null and 0. #2392

Closed
gabrielheming opened this issue Jun 3, 2022 · 4 comments
Labels
duplicate This issue or pull request already exists

Comments

@gabrielheming
Copy link

I encountered this issue while working on my project.
It is a bit specific, and as far as I could investigate, it is related to directly to npgsql/efcore.pg than to dotnet/efcore itself.

The summarized scenario is the following:
When inserting an entity that has an enum as a property, this property has a default value that is different than the enum member that has 0 as value, it is not possible to insert the entity setting the property to the enum member that corresponds to 0.

I have encountered only one entry about this issue on stackoverflow. Also, I have look it up here on the issues, and I haven't found anything related.

I've made a repo to test/reproduce this behaviour. It test against PostgreSQL and InMemory database.
https://github.com/gabrielheming/enum-default-value-error

I hadn't time to test on different databases to be sure that it is specific here, but I think it can give you everything that is necessary.

To exemplify:

Entity

public class MyEntity
{
    public int MyEntityId { get; set; }
    public MyEnum MyEnum { get; set; }
}

Enum

public enum MyEnum
{
    First, // implicit value = 0
    Second // implicit value = 1
}

DbContext

public class AppDbContext : DbContext
{
    public DbSet<MyEntity> MyEntities { get; set; }

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }

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

        modelBuilder.Entity<MyEntity>()
            .Property(_ => _.MyEnum)
            .HasDefaultValue(MyEnum.Second)
            .HasConversion<string>();
    }
}

Ps.: I don't think converting to string adds any difference to the output, but I replicate as I had been using on my project.

Execution

var entity = new MyEntity()
{
     MyEnum = MyEnum.First
 };

context.MyEntities.Add(entity);
context.SaveChanges();

Expected result:
entity.MyEnum // First

Actual result:
entity.MyEnum // Second

Workaround to solve this problem

Change the MyEnum order to make sure that the default value is 0:

public enum MyEnum
{
    Second, // implicit value = 0
    First // implicit value = 1
}

Set the first item of the Enum as value 1:

public enum MyEnum
{
    First = 1,
    Second // implicit value = 2
}
@roji
Copy link
Member

roji commented Jun 4, 2022

Duplicate of dotnet/efcore#15070

@roji roji marked this as a duplicate of #701 Jun 4, 2022
@roji
Copy link
Member

roji commented Jun 4, 2022

See also dotnet/efcore#15070 which is specifically about warning for enums; that issue contains lots of explanations about what's going on and possible workarounds.

@roji roji added the duplicate This issue or pull request already exists label Jun 4, 2022
@gabrielheming
Copy link
Author

May I ask you where I can find about #15070, there is no link about it. Also, I couldn't find the relation between this issue and the #701. I'm not saying that this isn't a duplication, it is just seems unrelated.

@roji roji marked this as a duplicate of dotnet/efcore#15070 Jun 5, 2022
@roji roji marked this as not a duplicate of #701 Jun 5, 2022
@roji
Copy link
Member

roji commented Jun 5, 2022

Sorry, I messed up the links above - they should have been to the EF Core repo. I've corrected them.

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants