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

efcore2 warns about default values for bool #9291

Closed
joeaudette opened this issue Jul 28, 2017 · 10 comments
Closed

efcore2 warns about default values for bool #9291

joeaudette opened this issue Jul 28, 2017 · 10 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@joeaudette
Copy link

I'm in the process of updating a large app from efcore 1.1 to efcore 2preview
Got everything working with the existing models and previous migrations but ran into this problem when adding new properties to the model.

I added a few properties to my model and then wanted to generate a migration. The new props are added like this:

entity.Property(p => p.PwdRequireNonAlpha)
            .IsRequired()
            .HasColumnType("bit")
            .HasDefaultValue(true)
            ;
            entity.Property(p => p.PwdRequireLowercase)
            .IsRequired()
            .HasColumnType("bit")
            .HasDefaultValue(true)
            ;
            entity.Property(p => p.PwdRequireUppercase)
            .IsRequired()
            .HasColumnType("bit")
            .HasDefaultValue(true)
            ;
            entity.Property(p => p.PwdRequireDigit)
            .IsRequired()
            .HasColumnType("bit")
            .HasDefaultValue(true)
            ;

Then when I generated the migration I got a lot of warnings about default values for existing bool properties and also about my new ones like this:

The 'bool' property 'PwdRequireNonAlpha' on entity type 'SiteSettings' is configured with a 
database-generated default. This default will always be used when the property has the value 'false', 
since this is the CLR default for the 'bool' type. Consider using the nullable 'bool?' type instead so that
 the default will only be used when the property value is 'null'.

So that means if later I update the value to false on the entity it will be changed to true?
I don't want a nullable bool, what I want is to add the column and for existing rows make it true.

How can I accomplish that?

@joeaudette
Copy link
Author

I am really confused by these warnings about default bool values. Based on the warning it sounds like if my entity has false and the db default is true it would save true when the entity is saved. But testing this with the latest preview it seems to not do that, I can save the entity with a false value even though the database default is true.

So is the warning incorrect? I don't get it

@joeaudette
Copy link
Author

Maybe these warnings only apply on insert? If so that would make more sense to me and not be so scary. If that is true maybe the warning should indicate that because the way it reads now it sounds like it would happen any time the entity is saved, but from testing it works fine when updating.

@joeaudette
Copy link
Author

From testing it does in fact seem that those warnings only apply for inserts of new entities. If the db default of a bool is true but the entity being created has the property as false, it will be true after the entity is created.
But subsequent saves of the entity can save the property as false.

My feedback is that the messages would be less scary if they mentioned the issue affects only inserts.

Closing the issue

@smitpatel smitpatel reopened this Jul 31, 2017
@smitpatel
Copy link
Contributor

We should refine the error message to indicate it is insert time only.

@divega divega added this to the 2.1.0 milestone Aug 2, 2017
@ajcvickers
Copy link
Contributor

@joeaudette For cases where you want to set a default value for use in existing rows when migrating the database, but don't want EF to use the default value for any newly inserted rows you can do this:

entity.Property(p => p.PwdRequireNonAlpha)
   .HasDefaultValue(true)
   .ValueGeneratedNever();

This tells EF that even though the column has a default, the EF runtime should not make use of that default.

@joeaudette
Copy link
Author

@ajcvickers thanks for the tip! That is great, does that also eliminate the warning?

What I did to solve it was first add the default as true, generate a migration, then remove the default and generate another migration, and that also worked as the first migration made sure existing rows got true and then the second migration removed the default and eliminated the warnings.

@ajcvickers
Copy link
Contributor

@joeaudette Not sure if it removes the warning, but I will check when I do the work to update the error message.

@cgountanis
Copy link

cgountanis commented Oct 10, 2017

This warning is confusing. Maybe I want to have a bool not nullable with default value false within SQL Server, defaultvalue ((0)). So when data is added, from app or manually, it automatically sets to false. I do not want a bool? and this warning is not valuable. Am I doing something wrong?

[Warning] The 'bool' property '"Disabled"' on entity type '"SystemUser"' is configured with a database-generated default. This default will always be used when the property has the value 'false', since this is the CLR default for the 'bool' type. Consider using the nullable 'bool?' type instead so that the default will only be used when the property value is 'null'. {EventId: { Id: 200601, Name: "Microsoft.EntityFrameworkCore.Model.Validation.BoolWithDefaultWarning" }, SourceContext: "Microsoft.EntityFrameworkCore.Model.Validation"}

@smitpatel
Copy link
Contributor

@cgountanis - Have a look at issue #9627 which matches up with your scenario.

@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
ajcvickers added a commit that referenced this issue Feb 26, 2018
Issues:
#221 Change tracker logging
#9291 Non-nullable bool warning--only inserts, and only if constraint is used by EF
#9761 Naked store type shows property
#10170 Conceptual null message
#10435 Cascade delete logging
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 26, 2018
ajcvickers added a commit that referenced this issue Feb 27, 2018
Issues:
#221 Change tracker logging
#9291 Non-nullable bool warning--only inserts, and only if constraint is used by EF
#9761 Naked store type shows property
#10170 Conceptual null message
#10435 Cascade delete logging
@vertonghenb
Copy link

@joeaudette it removes the warning.

@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

6 participants