8

I am using EF Core Power Tools version 2.4.0 with Miccrosoft.EntifyFrameworkCore.SqlServer version 2.2.6

I have SQL table column IsActive defined as [IsActive] [bit] NOT NULL
I use EF Core Power Tool's reverse engineering to generate entities and DB Context.

ISSUE
The tool generate null-able Boolean property instead of just Boolean

public bool? IsActive { get; set; }

the corresponding DBContext's OnModelCreating method

modelBuilder.Entity<Scenario>(entity =>
            {
                entity.Property(e => e.ScenarioID).HasColumnName("ScenarioID");

                entity.Property(e => e.IsActive)
                    .IsRequired()
                    .HasDefaultValueSql("((1))");

}
LP13
  • 30,567
  • 53
  • 217
  • 400

2 Answers2

5

EF Core uses the CLR default value to determine whether to use the SQL default.

With nullable:

  • null ➡ 1 (via DEFAULT)
  • false ➡ 0
  • true ➡ 1

Without nullable:

  • false ➡ 1 (via DEFAULT)
  • true ➡ 1

Without nullable, there would be no way to insert a 0!

Another alternative is to just remove HasDefaultValueSql and use non-nullable:

  • false ➡ 0
  • true ➡ 1
bricelam
  • 28,825
  • 9
  • 92
  • 117
  • 2
    don't understand `Without nullable, there would be no way to insert a 0!` you can insert `0` in not null column – LP13 Feb 26 '20 at 19:02
  • 2
    Correct, you can outside of EF Core. Inside EF Core, however, a non-nullalbe bool has two values--false and true--and EF Core uses the CLR default (false) to indicate that you want to use the SQL default (1). There would be no way to tell EF to insert 0. – bricelam Feb 26 '20 at 22:12
  • Note that it's sill marked as `IsRequired`--you can't actually save NULL values to the database. It just means that if you save a new entity without setting a value, the SQL default (1) will be used. – bricelam Feb 26 '20 at 22:16
  • 2
    @bricelam I wonder one thing about this.. why not have a column that is `X BIT NOT NULL DEFAULT 1` be mapped as `public bool X { get; set; } = true;` then you can save a 0 by setting it false but by default it is inited as true so the C# apes the SQL behavior? (Why do we need a way to say to EF "i want to use the DB default here" vs copying DB defaults to C#? OK, it doesn't allow the DBA to alter them without recompiling C# but that seems like asking for trouble anyway.. Could allow in other ways than nullabools too (`isSet=false` bool that `true`s upon `set{}` or do `bool?->bool` in `get{}`) – Caius Jard Sep 02 '21 at 17:14
  • 2
    @CaiusJard Great minds think alike. See [#15070 (comment)](https://github.com/dotnet/efcore/issues/15070#issuecomment-477220843) – bricelam Sep 02 '21 at 22:16
3

You can disable this behavior in the latest release of EF Core Power Tools, so the default is ignored

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • 3
    Thanks! My extension was disabled upon updating but got it figured out. For others wondering, the setting for this is to check "Remove SQL default from bool columns" under Reverse Engineer > Advanced. – johnw182 Oct 05 '22 at 18:49