Skip to content

bit not null created as bool? #160

Closed

Description

@mike-at-sivity-ai

When a table has column with type bit not null EFCore PowerTools generates as bool? instead of bool

Steps to reproduce

Create table with bit not null column and reverse engineer

Further technical details

EF Core Power Tools version: 2.2.2.0

Database engine: SqlServer

Visual Studio version: Visual Studio Ent 2017 15.9.5

Activity

  1. ErikEJ commented on Jan 9, 2019

    @ErikEJ
    Owner

    Hmmm. That should have been fixed in 2.2 - can you please share a CREATE TABLE script, thanks

  2. ErikEJ commented on Jan 10, 2019

    @ErikEJ
    Owner

    Ping?

  3. mike-at-sivity-ai commented on Jan 10, 2019

    @mike-at-sivity-ai
    Author

    Sorry for the delay have been travelling.
    `
    USE [Test]
    GO

    /****** Object: Table [dbo].[Test] Script Date: 11-Jan-19 11:37:32 AM ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[Test](
    [ID] [int] NOT NULL,
    [TestBool] [bit] NOT NULL,
    CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
    (
    [ID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO

    ALTER TABLE [dbo].[Test] ADD CONSTRAINT [DF_Test_TestBool] DEFAULT ((1)) FOR [TestBool]
    GO

    `

    Test.cs
    `
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    namespace EFBitTest.Model
    {
    [Table("Test")]
    public partial class Test
    {
    [Column("ID")]
    public int ID { get; set; }
    [Required]
    public bool? TestBool { get; set; }
    }
    }`

    efpt.config.json
    { "ContextClassName": "TestContext", "DefaultDacpacSchema": null, "IdReplace": true, "IncludeConnectionString": true, "OutputPath": "Model", "ProjectRootNamespace": "EFBitTest", "SelectedToBeGenerated": 0, "Tables": [ { "HasPrimaryKey": true, "Name": "dbo.Test" } ], "UseDatabaseNames": false, "UseFluentApiOnly": false, "UseHandleBars": false, "UseInflector": true }

  4. bricelam commented on Jan 10, 2019

    @bricelam
    Contributor
    TestBool bit NOT NULL DEFAULT 1

    This is by-design in EF Core. If the property wasn't nullable, there would be no way to store a 0.

    Property type SaveChanges() when null when false true
    bool (won't compile) 1 (from DEFAULT) 1
    bool? 1 (from DEFAULT) 0 1
  5. bricelam commented on Jan 10, 2019

    @bricelam
    Contributor

    Note, if you remove the HasDefaultValueSql() configuration, you can make the property non-nullable.

  6. ErikEJ commented on Jan 11, 2019

    @ErikEJ
    Owner

    Closing as by design

  7. added this to the efcore-22 milestone on Jan 11, 2019
  8. reopened this on Dec 18, 2020
  9. removed this from the efcore-22 milestone on Dec 18, 2020
  10. added a commit that references this issue on Dec 18, 2020
    02d843c
  11. added a commit that references this issue on Dec 18, 2020
    3def168
  12. ErikEJ commented on Dec 18, 2020

    @ErikEJ
    Owner

    @rationalmonkey @mike-upshon @smatvienko @rcfearn @StefanFalz @roopeshkumar

    Fixed in latest daily build (two years in the making) 😄

    image

  13. mikerentmeister commented on Feb 12, 2021

    @mikerentmeister

    @ErikEJ - Would you be willing to add "Remove default constraint from bool columns" as a cli option?

  14. ErikEJ commented on Feb 12, 2021

    @ErikEJ
    Owner

    To what CLI??

  15. mikerentmeister commented on Feb 12, 2021

    @mikerentmeister

    @ErikEJ
    We currently run this tool from the cli via the following command:
    dotnet ef dbcontext scaffold

    Listed here: https://github.com/ErikEJ/EFCorePowerTools/wiki/Reverse-Engineering#generate-poco-classes-derived-dbcontext-and-mapping-for-an-existing-database

  16. mikerentmeister commented on Feb 12, 2021

    @mikerentmeister

    Our specific use case for this flag comes into play with EF Migrations. When you add a bit column to a table with existing data, you need to provide a sql default. Every time we create a new migration, we run this tool to update our POCOs, but it keeps converting our columns to nullable booleans.

  17. ErikEJ commented on Feb 12, 2021

    @ErikEJ
    Owner

    The repository is not that tool, that tool is in the official EF Core repository.

    Suggest you post the request there, feel free to refer to this issue!

  18. mikerentmeister commented on Feb 12, 2021

    @mikerentmeister

    Ah, apologies, I came from the EF Core repo, but didn't realized I had switched repos.

  19. ErikEJ commented on Feb 12, 2021

    @ErikEJ
    Owner

    No worries, be aware that this feature is available in EF Core Power Tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions