Skip to content

Update handling of non-nullable store-generated properties #15070

Description

@JakePorter05

If you make the default value of an enum that is mapped to the database anything other then 0 it always defaults every value saved to the default value.

Example with code: If I do default sleeping and try to set it to active and save. It will change it back to sleeping in the savechanges. But if you leave the default at 0 aka active then you can change and save as normal.

EfcoreEnumTester.zip

public partial class Person
{
   [Key, Column(TypeName = "uniqueidentifier")]
   public Guid Id { get; set; }
   [Column(TypeName = "nvarchar(20)")]
   public string FirstName { get; set; }
   [Column(TypeName = "nvarchar(20)")]
   public string LastName { get; set; }
   [Column(TypeName = "int")]
   public State State { get; set; }
 }

public enum State
{
    Active = 0,
    Sleeping = 1,
}

modelBuilder.Entity<Person>(entity =>
{
    entity.HasIndex(e => e.Id);
    entity.Property(e => e.State)
              .HasConversion<int>()
              .HasDefaultValue(State.Sleeping);
 });

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions