EF Core does not include boolean column with default value in SQL INSERT statement
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:
- Create an entity with a boolean property.
- Configure the boolean property to have a default value of true.
- Add a new entity with the boolean property set to false.
- 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
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.
Duplicate of #701
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 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
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: @.***>