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

Behaviour of bool fields with HasDefaultValue option #6054

Closed
crabulik opened this issue Jul 12, 2016 · 5 comments
Closed

Behaviour of bool fields with HasDefaultValue option #6054

crabulik opened this issue Jul 12, 2016 · 5 comments

Comments

@crabulik
Copy link

crabulik commented Jul 12, 2016

Hi. I need to clear such situation.
I have an entity Product with bool field:

public bool IsActive{ get; set; }

For that field I made such definition in my DbCOntext:

protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Product>() .Property(b => b.IsActive) .HasDefaultValue(true); }

So, if I insert the Product with "IsActive=false", the record wor this Product in DB will contain "IsActive=true".

Is it correct EF behaviour?

Because in documentation it says:

The default value of a column is the value that will be inserted if a new row is inserted but no value is specified for the column.

Because I think that "IsActive=false" is a specified value.

@ajcvickers
Copy link
Contributor

@crabulik The default value is used when the property value is the CLR default for the type. False is the CLR default, so the default value is used. You probably want to use a nullable bool. Then the default will only get used if the value is null.

@divega
Copy link
Contributor

divega commented Nov 23, 2016

Reopening to consider adding a model validation that errors or warns when this scenario is hit.

@divega divega reopened this Nov 23, 2016
@divega
Copy link
Contributor

divega commented Nov 23, 2016

There seems to be more useful information and discussion now in #7089 so closing this issue as a dupe of that one.

@Linnod
Copy link

Linnod commented Apr 5, 2019

Hello!

This problem is still actual. If there's code like builder.Property(x => x.IsActive).HasColumnName("IsActive").HasDefaultValue(true); in entity's configuration and IsActive value equals FALSE, EF does not generate insert for this column.

Thank you!

@m4ss1m0g
Copy link
Contributor

m4ss1m0g commented Jan 2, 2020

I found a solution from stackoverflow post for REQUIRED boolean field and default value

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
        modelBuilder.Entity<MyEntity>()
             .Property(p => p.MyRequiredBooleanField)
             .ValueGeneratedNever()
             .HasDefaultValue(false);
}

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants