question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reverse engineering a non-nullable bit column with DEFAULT constraint incorrectly generates nullable value type (i.e., bool? rather than bool)

See original GitHub issue

A bit NULL column correctly generates a property typed as bool. A bit NOT NULL column correctly generates a property typed as bool?. A bit DEFAULT ((1)) NOT NULL column incorrectly generates a property typed as bool?.

If a non-nullable bit column has a DEFAULT value, then it should generate the non-nullable value type bool, not bool?.

This is wreaking havoc with our codebase, where developers are adding undesirable and error-prone code, such as needlessly testing .HasValue, and/or adding needless coalesce (i.e., if (x.IsActive ?? false) rather than if (x.IsActive).

Provide steps to reproduce

Create a table with three bit columns; for example: CREATE TABLE MyTable ( NB bit NULL, NNB bit NOT NULL, NNBD bit DEFAULT (1) NOT NULL)

Then reverse-engineer the table and notice the generated code:

public bool? NB { get; set; } <- correct public bool NNB { get; set; } <- correct public bool? NNBD { get; set; } <- incorrect, should be bool

See image below.

Provide technical details

  • EF Core version in use: 3.1.4

  • Is Handlebars used: I think so (not sure what this means, but Customize code using Handlebars templates C# is checked).

  • Is .dacpac used: I think so (the generator source is a .sqlproj project, not the actual database). However, I have confirmed that this issue occurs whether generating from the .sqlproj project or the actual database).

  • EF Core Power Tools version: 2.5.1020.0

  • Database engine: SQL Server 2019

  • Visual Studio version: Visual Studio 2022

EFCorePowerTools-bug

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bricelamcommented, Jun 29, 2022

EF uses the CLR default (false) to determine that a value hasn’t been set by the user.

// These two lines are identical to EF
context.Add(new MyTable());
context.Add(new MyTable { NNBD = false });

If a value hasn’t been set, and the property has a default constraint EF won’t send the value to the database so the DEFAULT constraint will be used.

Since the default value is 1, that value is propagated back to the objects after calling SaveChanges. Hence, whether you specify false or true, the result is always true.

Introducing a nullable bool helps EF differentiate between the two calls above and allows you to insert both true and false.

1reaction
ErikEJcommented, Jun 29, 2022

LOL - You should ask the EF Core team for a more explicit explanation!

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core power tools generate nullable Boolean
I use EF Core Power Tool's reverse engineering to generate entities and DB Context. ISSUE The tool generate null-able Boolean property instead ......
Read more >
Working with nullable reference types - EF Core
Exercise caution when enabling nullable reference types on an existing project: reference type properties which were previously configured as ...
Read more >
Solution - Non-nullable Property Must Contain a Non-null ...
Non-nullable property must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Read more >
MySQL Bugs: #51491: Reverse Engineer SQL Script wizard
Description: the Reverse Engineer SQL Script wizard does not accept the BOOL or BOOLEAN data types for columns. Columns are created but do ......
Read more >
database design - Why shouldn't we allow NULLs?
Firstly I want to set something straight for all future readers... NULL values represent unknown data NOT unused data. If you have an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found