-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
@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. |
Reopening to consider adding a model validation that errors or warns when this scenario is hit. |
There seems to be more useful information and discussion now in #7089 so closing this issue as a dupe of that one. |
Hello! This problem is still actual. If there's code like Thank you! |
I found a solution from stackoverflow post for REQUIRED boolean field and default value
|
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:
Because I think that "IsActive=false" is a specified value.
The text was updated successfully, but these errors were encountered: