Efcore: Consider using the nullable 'bool?' - Warning Annoyance

Created on 1 Jun 2018  路  7Comments  路  Source: dotnet/efcore

CORE SDK 2.1.300

2018-06-01 01:12:22.510 -05:00 [Warning] The 'bool' property '"Deleted"' on entity type '"DataAttachment"' is configured with a database-generated default. This default will always be used for inserts 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 for inserts when the property value is 'null'. Microsoft.EntityFrameworkCore.Model.Validation

Model

[Required]
public bool Deleted { get; set; } 

I can fix it by uncommenting this option in my AddDbContext options.

// Add DbContext using connection string from settings.
services.AddDbContext<Context>(options =>
{
    options.UseSqlServer(_configuration.GetConnectionString("DefaultConnection"));
    //options.ConfigureWarnings(wc => wc.Ignore(RelationalEventId.BoolWithDefaultWarning));
});

I thought this was going to be fixed? Do I have to add an annotation or default value to all my bool properties? I expect and want them to be false and even set a default value of false on those fields.

builder.Property(o => o.Deleted).HasDefaultValue(false);

closed-fixed customer-reported type-enhancement

Most helpful comment

Triage: we should not generate this warning if the database default is false because when the default value is false the outcome will be the same regardless of whether false is inserted or false is generated by the store. We will still generate the warning for true.

All 7 comments

@cgountanis What version of EF Core are you using? (Note that this may not be the same as the SDK version.)

I only have the CORE 2.1.300 SDK installed to keep things simple as I upgrade to make sure nothing gets funky. I get a bit confused on NUGET vs. SDK, at times it is not correct seems to be getting better.

image

>dotnet --version
2.1.300
>dotnet ef --version
Entity Framework Core .NET Command-line Tools
2.1.0-rtm-30799
<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.0" />
</ItemGroup>

I even removed my migrations and started fresh with a new Add-Migration IntitalCreate, Update-Database just to test the newer versions.

Did you reverse engineer the model using Scaffold-DbContext/dotnet ef dbcontext scaffold?

@bricelam No as stated, I am doing code-first with Add-Migration unless I am misunderstanding the question.

Triage: we should not generate this warning if the database default is false because when the default value is false the outcome will be the same regardless of whether false is inserted or false is generated by the store. We will still generate the warning for true.

So does this mean in future release I will not need to suppress the warning with the code (options.ConfigureWarnings) that I have provided?

@cgountanis Correct.

Was this page helpful?
0 / 5 - 0 ratings