efcore icon indicating copy to clipboard operation
efcore copied to clipboard

EF Core does not include boolean column with default value in SQL INSERT statement

Open Gurgen opened this issue 3 years ago • 5 comments

When I set a boolean property with a default value in my entity and then try to add a new record with a value different from the default, EF Core generates an SQL INSERT statement without including that column.

Steps to reproduce:

  1. Create an entity with a boolean property.
  2. Configure the boolean property to have a default value of true.
  3. Add a new entity with the boolean property set to false.
  4. Notice that the generated SQL does not include the boolean column.

Expected behavior: The SQL statement should include the boolean column with the value set to false.

Actual behavior: The SQL statement does not include the boolean column, and the default value of true is always set.

EF Core version: 6.0.8 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6

Gurgen avatar Aug 23 '23 10:08 Gurgen

See #7089. I've tried my best to explain why it's maddening. Apparently it's too deeply rooted in EF's logic based on CLR defaults to fix it.

GertArnold avatar Aug 23 '23 11:08 GertArnold

Duplicate of #701

ajcvickers avatar Sep 05 '23 16:09 ajcvickers

Sorry , I've the same problem, EF Core 8.0.8 not include on insert statement a short value that have Constraints default value 1, but I need to insert 0 , and this is not taken into account. My fields don't exist in final statement INSERT INTO [TabRelPacchettiSiti] ([IdPacchetto], [IdSito], [OrdineAttivazione], [DataEnd], [DataStart], [Ender], [Owner]) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6); SELECT [PacchettoAttivo] FROM [TabRelPacchettiSiti] WHERE @@ROWCOUNT = 1 AND [IdPacchetto] = @p0 AND [IdSito] = @p1 AND [OrdineAttivazione] = @p2;

EgleMir avatar Mar 21 '25 17:03 EgleMir

@EgleMir if you follow the issue that this was marked as a duplicate of, you can see it was completed for EF 8. See here for more details: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#sentinel-values-and-database-defaults

stevendarby avatar Mar 23 '25 18:03 stevendarby

Oh thanks @stevendarby, that's exactly my case, unfortunately googling there were at least ten issues on this topic and honestly it was not clear if the question had been resolved or not. It's a bit too dispersive.

Il giorno dom 23 mar 2025 alle ore 19:52 Steven Darby < @.***> ha scritto:

@EgleMir https://github.com/EgleMir if you follow the issue that this was marked as a duplicate of, you can see it was completed for EF 8. See here for more details: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#sentinel-values-and-database-defaults

— Reply to this email directly, view it on GitHub https://github.com/dotnet/efcore/issues/31538#issuecomment-2746364548, or unsubscribe https://github.com/notifications/unsubscribe-auth/AR4YODK6AEB24OUMD2JFUAL2V37IDAVCNFSM6AAAAABZQR4VFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONBWGM3DINJUHA . You are receiving this because you were mentioned.Message ID: @.***> [image: stevendarby]stevendarby left a comment (dotnet/efcore#31538) https://github.com/dotnet/efcore/issues/31538#issuecomment-2746364548

@EgleMir https://github.com/EgleMir if you follow the issue that this was marked as a duplicate of, you can see it was completed for EF 8. See here for more details: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#sentinel-values-and-database-defaults

— Reply to this email directly, view it on GitHub https://github.com/dotnet/efcore/issues/31538#issuecomment-2746364548, or unsubscribe https://github.com/notifications/unsubscribe-auth/AR4YODK6AEB24OUMD2JFUAL2V37IDAVCNFSM6AAAAABZQR4VFKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDONBWGM3DINJUHA . You are receiving this because you were mentioned.Message ID: @.***>

EgleMir avatar Mar 24 '25 08:03 EgleMir