Skip to content

Commit 81c2440

Browse files
authored
No longer scaffold nullable booleans when column has a default value (#31148)
1 parent e88e4d1 commit 81c2440

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

Diff for: src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ protected virtual EntityTypeBuilder VisitColumns(EntityTypeBuilder builder, ICol
391391
}
392392

393393
if (clrType == typeof(bool)
394-
&& column.DefaultValueSql != null)
394+
&& column.DefaultValueSql != null
395+
&& column.DefaultValue == null)
395396
{
396397
_reporter.WriteWarning(
397398
DesignStrings.NonNullableBoooleanColumnHasDefaultConstraint(column.DisplayName()));

Diff for: test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs

+55-1
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,7 @@ public void Pluralization_of_collection_navigations_noPluralize()
18651865
}
18661866

18671867
[ConditionalFact]
1868-
public void Not_null_bool_column_with_default_value_is_made_nullable()
1868+
public void Not_null_bool_column_with_unparsed_default_value_is_made_nullable()
18691869
{
18701870
var dbModel = new DatabaseModel
18711871
{
@@ -1910,6 +1910,60 @@ public void Not_null_bool_column_with_default_value_is_made_nullable()
19101910
Assert.Equal("Default", columns.First(c => c.Name == "NonNullBoolWithDefault")[RelationalAnnotationNames.DefaultValueSql]);
19111911
}
19121912

1913+
[ConditionalFact]
1914+
public void Not_null_bool_column_with_parsed_default_value_is_not_made_nullable()
1915+
{
1916+
var dbModel = new DatabaseModel
1917+
{
1918+
Tables =
1919+
{
1920+
new DatabaseTable
1921+
{
1922+
Database = Database,
1923+
Name = "Table",
1924+
Columns =
1925+
{
1926+
IdColumn,
1927+
new DatabaseColumn
1928+
{
1929+
Table = Table,
1930+
Name = "NonNullBoolWithDefault",
1931+
StoreType = "bit",
1932+
DefaultValueSql = "1",
1933+
DefaultValue = true,
1934+
IsNullable = false
1935+
},
1936+
new DatabaseColumn
1937+
{
1938+
Table = Table,
1939+
Name = "NonNullBoolWithoutDefault",
1940+
StoreType = "bit",
1941+
IsNullable = false
1942+
}
1943+
},
1944+
PrimaryKey = IdPrimaryKey
1945+
}
1946+
}
1947+
};
1948+
1949+
var model = _factory.Create(dbModel, new ModelReverseEngineerOptions());
1950+
1951+
var columns = model.FindEntityType("Table")!.GetProperties().ToList();
1952+
var columnWithDefault = columns.First(c => c.Name == "NonNullBoolWithDefault");
1953+
var columnWithoutDefault = columns.First(c => c.Name == "NonNullBoolWithoutDefault");
1954+
1955+
Assert.Equal(typeof(bool), columnWithoutDefault.ClrType);
1956+
Assert.False(columnWithoutDefault.IsNullable);
1957+
Assert.Equal(typeof(bool), columnWithDefault.ClrType);
1958+
Assert.False(columnWithDefault.IsNullable);
1959+
Assert.Equal("1", columnWithDefault[RelationalAnnotationNames.DefaultValueSql]);
1960+
Assert.Equal(true, columnWithDefault[RelationalAnnotationNames.DefaultValue]);
1961+
Assert.Null(columnWithoutDefault[RelationalAnnotationNames.DefaultValueSql]);
1962+
Assert.Null(columnWithoutDefault[RelationalAnnotationNames.DefaultValue]);
1963+
1964+
Assert.Empty(_reporter.Messages);
1965+
}
1966+
19131967
[ConditionalFact]
19141968
public void Nullable_column_with_default_value_sql_does_not_generate_warning()
19151969
{

0 commit comments

Comments
 (0)