From db2c056b9fe31696741f408ca77bc6a9096288d3 Mon Sep 17 00:00:00 2001 From: Laurents Meyer Date: Mon, 24 May 2021 07:21:16 +0200 Subject: [PATCH] Return default value for JetDateTimeTypeMapping derived classes as well. (#102) --- .../Migrations/JetMigrationsSqlGenerator.cs | 20 +++++++++---------- .../Internal/JetDateTimeOffsetTypeMapping.cs | 7 ++----- .../Internal/JetDateTimeTypeMapping.cs | 13 +++++++----- .../Internal/JetTimeSpanTypeMapping.cs | 4 ++-- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/EFCore.Jet/Migrations/JetMigrationsSqlGenerator.cs b/src/EFCore.Jet/Migrations/JetMigrationsSqlGenerator.cs index ab80136..159c383 100644 --- a/src/EFCore.Jet/Migrations/JetMigrationsSqlGenerator.cs +++ b/src/EFCore.Jet/Migrations/JetMigrationsSqlGenerator.cs @@ -771,18 +771,16 @@ namespace Microsoft.EntityFrameworkCore.Migrations } else if (defaultValue != null) { - var typeMapping = columnType != null - ? Dependencies.TypeMappingSource.FindMapping(defaultValue.GetType(), columnType) - : null; - if (typeMapping == null) - { - typeMapping = Dependencies.TypeMappingSource.GetMappingForValue(defaultValue); - } - - defaultValue = defaultValue.GetType().IsTimeRelatedType() - ? JetDateTimeTypeMapping.GenerateNonNullSqlLiteral(defaultValue, true, _options.EnableMillisecondsSupport) + var typeMapping = (columnType != null + ? Dependencies.TypeMappingSource.FindMapping(defaultValue.GetType(), columnType) + : null) ?? + Dependencies.TypeMappingSource.GetMappingForValue(defaultValue); + + // All time related type mappings derive from JetDateTimeTypeMapping. + defaultValue = typeMapping is JetDateTimeTypeMapping dateTimeTypeMapping + ? dateTimeTypeMapping.GenerateNonNullSqlLiteral(defaultValue, defaultClauseCompatible: true) : typeMapping.GenerateSqlLiteral(defaultValue); - + builder .Append(" DEFAULT ") .Append(defaultValue); diff --git a/src/EFCore.Jet/Storage/Internal/JetDateTimeOffsetTypeMapping.cs b/src/EFCore.Jet/Storage/Internal/JetDateTimeOffsetTypeMapping.cs index 337060b..83d1732 100644 --- a/src/EFCore.Jet/Storage/Internal/JetDateTimeOffsetTypeMapping.cs +++ b/src/EFCore.Jet/Storage/Internal/JetDateTimeOffsetTypeMapping.cs @@ -45,10 +45,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal base.ConfigureParameter(parameter); } - protected override string GenerateNonNullSqlLiteral(object value) - => base.GenerateNonNullSqlLiteral( - value is DateTimeOffset dateTimeOffset - ? dateTimeOffset.UtcDateTime - : value); + protected override DateTime ConvertToDateTimeCompatibleValue(object value) + => ((DateTimeOffset) value).UtcDateTime; } } \ No newline at end of file diff --git a/src/EFCore.Jet/Storage/Internal/JetDateTimeTypeMapping.cs b/src/EFCore.Jet/Storage/Internal/JetDateTimeTypeMapping.cs index 3f51ed0..0c36441 100644 --- a/src/EFCore.Jet/Storage/Internal/JetDateTimeTypeMapping.cs +++ b/src/EFCore.Jet/Storage/Internal/JetDateTimeTypeMapping.cs @@ -53,17 +53,17 @@ namespace EntityFrameworkCore.Jet.Storage.Internal } protected override string GenerateNonNullSqlLiteral(object value) - => GenerateNonNullSqlLiteral(value, false, _options.EnableMillisecondsSupport); + => GenerateNonNullSqlLiteral(value, false); - public static string GenerateNonNullSqlLiteral(object value, bool defaultClauseCompatible, bool millisecondsSupportEnabled) + public virtual string GenerateNonNullSqlLiteral(object value, bool defaultClauseCompatible) { - var dateTime = (DateTime) value; + var dateTime = ConvertToDateTimeCompatibleValue(value); dateTime = CheckDateTimeValue(dateTime); if (defaultClauseCompatible) { - return _decimalTypeMapping.GenerateSqlLiteral(GetDateTimeDoubleValueAsDecimal(dateTime, millisecondsSupportEnabled)); + return _decimalTypeMapping.GenerateSqlLiteral(GetDateTimeDoubleValueAsDecimal(dateTime, _options.EnableMillisecondsSupport)); } var literal = new StringBuilder() @@ -77,7 +77,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal literal.Append("#"); - if (millisecondsSupportEnabled && + if (_options.EnableMillisecondsSupport && time != TimeSpan.Zero) { // Round to milliseconds. @@ -97,6 +97,9 @@ namespace EntityFrameworkCore.Jet.Storage.Internal return literal.ToString(); } + protected virtual DateTime ConvertToDateTimeCompatibleValue(object value) + => (DateTime) value; + private static decimal GetDateTimeDoubleValueAsDecimal(DateTime dateTime, bool millisecondsSupportEnabled) { // diff --git a/src/EFCore.Jet/Storage/Internal/JetTimeSpanTypeMapping.cs b/src/EFCore.Jet/Storage/Internal/JetTimeSpanTypeMapping.cs index b365785..18f61b1 100644 --- a/src/EFCore.Jet/Storage/Internal/JetTimeSpanTypeMapping.cs +++ b/src/EFCore.Jet/Storage/Internal/JetTimeSpanTypeMapping.cs @@ -28,7 +28,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) => new JetTimeSpanTypeMapping(parameters, _options); - protected override string GenerateNonNullSqlLiteral(object value) - => base.GenerateNonNullSqlLiteral(JetConfiguration.TimeSpanOffset + (TimeSpan) value); + protected override DateTime ConvertToDateTimeCompatibleValue(object value) + => JetConfiguration.TimeSpanOffset + (TimeSpan) value; } } \ No newline at end of file