Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bit not null created as bool? #160

Closed
1984-mike opened this issue Jan 9, 2019 · 14 comments · Fixed by #671
Closed

bit not null created as bool? #160

1984-mike opened this issue Jan 9, 2019 · 14 comments · Fixed by #671
Labels
enhancement New feature or request reveng

Comments

@1984-mike
Copy link

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

@ErikEJ
Copy link
Owner

ErikEJ commented Jan 9, 2019

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

@ErikEJ
Copy link
Owner

ErikEJ commented Jan 10, 2019

Ping?

@1984-mike
Copy link
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 }

@bricelam
Copy link
Contributor

bricelam commented Jan 10, 2019

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

@bricelam
Copy link
Contributor

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

@ErikEJ
Copy link
Owner

ErikEJ commented Jan 11, 2019

Closing as by design

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 18, 2020

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

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

image

@mikerentmeister
Copy link

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

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 12, 2021

To what CLI??

@mikerentmeister
Copy link

mikerentmeister commented Feb 12, 2021

@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

@mikerentmeister
Copy link

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.

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 12, 2021

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!

@mikerentmeister
Copy link

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

@ErikEJ
Copy link
Owner

ErikEJ commented Feb 12, 2021

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
Labels
enhancement New feature or request reveng
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants