Skip to content

Boolean property with default '1' not working #33683

@POFerro

Description

@POFerro

Hi :),

I have a SQL server database with a table with bit column not null with default value = '1'
When I scaffold it with EF core 8.0.4 the bool property is no longer nullable as expected.
Yet When I insert a record in the table without filling the bool property it goes to the database with false value = '0' in sql server.
This is the code

var db = new TestEfContext();

db.TestTables.Add(new TestTable { Name = "Test" });
db.SaveChanges();

And my database table is created like this:

CREATE TABLE [dbo].[TestTable] (
    [Id]       INT           IDENTITY (1, 1) NOT NULL,
    [Name]     VARCHAR (200) NOT NULL,
    [TestBool] BIT           CONSTRAINT [DF_TestTable_TestBool] DEFAULT ((1)) NOT NULL,
    CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ([Id] ASC)
);

Please find attached a small solution with the test console app and the database project.
TestEF.zip

I'm using this configuration
EF Core version: 8.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.9.6

Am I missing something, can you help me?
Thanks and best regards
POFerro

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions