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

Use InvariantCulture for ChangeType() and decimal.TryParse() #35675

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ FROM [sys].[views] v
{
try
{
return Convert.ChangeType(defaultValueSql, type);
return Convert.ChangeType(defaultValueSql, type, CultureInfo.InvariantCulture);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Data.Sqlite;
Expand Down Expand Up @@ -429,7 +430,7 @@ private void ParseClrDefaults(DatabaseTable table)
{
try
{
column.DefaultValue = Convert.ChangeType(defaultValueSql, type);
column.DefaultValue = Convert.ChangeType(defaultValueSql, type, CultureInfo.InvariantCulture);
}
catch
{
Expand Down Expand Up @@ -471,7 +472,7 @@ private void ParseClrDefaults(DatabaseTable table)
column.DefaultValue = dateTimeOffset;
}
else if (type == typeof(decimal)
&& decimal.TryParse(defaultValueSql, out var decimalValue))
&& decimal.TryParse(defaultValueSql, CultureInfo.InvariantCulture, out var decimalValue))
{
column.DefaultValue = decimalValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.SqlServer.Diagnostics.Internal;
Expand Down Expand Up @@ -3939,6 +3940,7 @@ CREATE TABLE MyTable (
B decimal DEFAULT (0.0),
C decimal DEFAULT (0),
D decimal DEFAULT ((CONVERT ( ""decimal"", ( (1.1234) ) ))),
E decimal DEFAULT ((10.0)),
);",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
Expand All @@ -3962,11 +3964,70 @@ D decimal DEFAULT ((CONVERT ( ""decimal"", ( (1.1234) ) ))),
Assert.Equal("(CONVERT([decimal],(1.1234)))", column.DefaultValueSql);
Assert.Equal((decimal)1.1234, column.DefaultValue);

column = columns.Single(c => c.Name == "E");
Assert.Equal("((10.0))", column.DefaultValueSql);
Assert.Equal((decimal)10, column.DefaultValue);

var model = scaffoldingFactory.Create(dbModel, new ModelReverseEngineerOptions());
Assert.Equal(1, model.GetEntityTypes().Count());
},
"DROP TABLE MyTable;");

[ConditionalFact]
public void Simple_decimal_literals_are_parsed_for_HasDefaultValue_with_Danish_locale()
{
var currentCulture = CultureInfo.CurrentCulture;

try
{
CultureInfo.CurrentCulture = new CultureInfo("da-DK");
Test(
@"
CREATE TABLE MyTable (
Id int,
A decimal DEFAULT -1.1111,
B decimal DEFAULT (0.0),
C decimal DEFAULT (0),
D decimal DEFAULT ((CONVERT ( ""decimal"", ( (1.1234) ) ))),
E decimal DEFAULT ((10.0)),
);",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
(dbModel, scaffoldingFactory) =>
{
var columns = dbModel.Tables.Single().Columns;

var column = columns.Single(c => c.Name == "A");
Assert.Equal("((-1.1111))", column.DefaultValueSql);
Assert.Equal((decimal)-1.1111, column.DefaultValue);

column = columns.Single(c => c.Name == "B");
Assert.Equal("((0.0))", column.DefaultValueSql);
Assert.Equal((decimal)0, column.DefaultValue);

column = columns.Single(c => c.Name == "C");
Assert.Equal("((0))", column.DefaultValueSql);
Assert.Equal((decimal)0, column.DefaultValue);

column = columns.Single(c => c.Name == "D");
Assert.Equal("(CONVERT([decimal],(1.1234)))", column.DefaultValueSql);
Assert.Equal((decimal)1.1234, column.DefaultValue);

column = columns.Single(c => c.Name == "E");
Assert.Equal("((10.0))", column.DefaultValueSql);
Assert.Equal((decimal)10, column.DefaultValue);

var model = scaffoldingFactory.Create(dbModel, new ModelReverseEngineerOptions());
Assert.Equal(1, model.GetEntityTypes().Count());
},
"DROP TABLE MyTable;");
}
finally
{
CultureInfo.CurrentCulture = currentCulture;
}
}

[ConditionalFact]
public void Simple_bool_literals_are_parsed_for_HasDefaultValue()
=> Test(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Globalization;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
Expand Down Expand Up @@ -850,9 +851,10 @@ CREATE TABLE MyTable (
Id int,
A decimal DEFAULT '-1.1111',
B decimal DEFAULT ('0.0'),
C decimal DEFAULT ('0'));
C decimal DEFAULT ('0'),
D decimal DEFAULT ('10.0'));

INSERT INTO MyTable VALUES (1, '1.1', '1.2', '1.3');",
INSERT INTO MyTable VALUES (1, '1.1', '1.2', '1.3', '1.4');",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
dbModel =>
Expand All @@ -870,9 +872,62 @@ B decimal DEFAULT ('0.0'),
column = columns.Single(c => c.Name == "C");
Assert.Equal("'0'", column.DefaultValueSql);
Assert.Equal((decimal)0, column.DefaultValue);

column = columns.Single(c => c.Name == "D");
Assert.Equal("'10.0'", column.DefaultValueSql);
Assert.Equal((decimal)10, column.DefaultValue);
},
"DROP TABLE MyTable;");

[ConditionalFact]
public void Simple_decimal_literals_are_parsed_for_HasDefaultValue_with_Danish_locale()
{
var culture = CultureInfo.CurrentCulture;

try
{
CultureInfo.CurrentCulture = new CultureInfo("da-DK");

Test(
@"
CREATE TABLE MyTable (
Id int,
A decimal DEFAULT '-1.1111',
B decimal DEFAULT ('0.0'),
C decimal DEFAULT ('0'),
D decimal DEFAULT ('10.0'));

INSERT INTO MyTable VALUES (1, '1.1', '1.2', '1.3', '1.4');",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
dbModel =>
{
var columns = dbModel.Tables.Single().Columns;

var column = columns.Single(c => c.Name == "A");
Assert.Equal("'-1.1111'", column.DefaultValueSql);
Assert.Equal((decimal)-1.1111, column.DefaultValue);

column = columns.Single(c => c.Name == "B");
Assert.Equal("'0.0'", column.DefaultValueSql);
Assert.Equal((decimal)0, column.DefaultValue);

column = columns.Single(c => c.Name == "C");
Assert.Equal("'0'", column.DefaultValueSql);
Assert.Equal((decimal)0, column.DefaultValue);

column = columns.Single(c => c.Name == "D");
Assert.Equal("'10.0'", column.DefaultValueSql);
Assert.Equal((decimal)10, column.DefaultValue);
},
"DROP TABLE MyTable;");
}
finally
{
CultureInfo.CurrentCulture = culture;
}
}

[ConditionalFact]
public void Simple_bool_literals_are_parsed_for_HasDefaultValue()
=> Test(
Expand Down