Return default value for JetDateTimeTypeMapping derived classes as well. (#102)

pull/104/head
Laurents Meyer 5 years ago committed by GitHub
parent ad5799ffa6
commit db2c056b9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -771,18 +771,16 @@ namespace Microsoft.EntityFrameworkCore.Migrations
} }
else if (defaultValue != null) else if (defaultValue != null)
{ {
var typeMapping = columnType != null var typeMapping = (columnType != null
? Dependencies.TypeMappingSource.FindMapping(defaultValue.GetType(), columnType) ? Dependencies.TypeMappingSource.FindMapping(defaultValue.GetType(), columnType)
: null; : null) ??
if (typeMapping == null) Dependencies.TypeMappingSource.GetMappingForValue(defaultValue);
{
typeMapping = Dependencies.TypeMappingSource.GetMappingForValue(defaultValue); // All time related type mappings derive from JetDateTimeTypeMapping.
} defaultValue = typeMapping is JetDateTimeTypeMapping dateTimeTypeMapping
? dateTimeTypeMapping.GenerateNonNullSqlLiteral(defaultValue, defaultClauseCompatible: true)
defaultValue = defaultValue.GetType().IsTimeRelatedType()
? JetDateTimeTypeMapping.GenerateNonNullSqlLiteral(defaultValue, true, _options.EnableMillisecondsSupport)
: typeMapping.GenerateSqlLiteral(defaultValue); : typeMapping.GenerateSqlLiteral(defaultValue);
builder builder
.Append(" DEFAULT ") .Append(" DEFAULT ")
.Append(defaultValue); .Append(defaultValue);

@ -45,10 +45,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
base.ConfigureParameter(parameter); base.ConfigureParameter(parameter);
} }
protected override string GenerateNonNullSqlLiteral(object value) protected override DateTime ConvertToDateTimeCompatibleValue(object value)
=> base.GenerateNonNullSqlLiteral( => ((DateTimeOffset) value).UtcDateTime;
value is DateTimeOffset dateTimeOffset
? dateTimeOffset.UtcDateTime
: value);
} }
} }

@ -53,17 +53,17 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
} }
protected override string GenerateNonNullSqlLiteral(object value) 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); dateTime = CheckDateTimeValue(dateTime);
if (defaultClauseCompatible) if (defaultClauseCompatible)
{ {
return _decimalTypeMapping.GenerateSqlLiteral(GetDateTimeDoubleValueAsDecimal(dateTime, millisecondsSupportEnabled)); return _decimalTypeMapping.GenerateSqlLiteral(GetDateTimeDoubleValueAsDecimal(dateTime, _options.EnableMillisecondsSupport));
} }
var literal = new StringBuilder() var literal = new StringBuilder()
@ -77,7 +77,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
literal.Append("#"); literal.Append("#");
if (millisecondsSupportEnabled && if (_options.EnableMillisecondsSupport &&
time != TimeSpan.Zero) time != TimeSpan.Zero)
{ {
// Round to milliseconds. // Round to milliseconds.
@ -97,6 +97,9 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
return literal.ToString(); return literal.ToString();
} }
protected virtual DateTime ConvertToDateTimeCompatibleValue(object value)
=> (DateTime) value;
private static decimal GetDateTimeDoubleValueAsDecimal(DateTime dateTime, bool millisecondsSupportEnabled) private static decimal GetDateTimeDoubleValueAsDecimal(DateTime dateTime, bool millisecondsSupportEnabled)
{ {
// //

@ -28,7 +28,7 @@ namespace EntityFrameworkCore.Jet.Storage.Internal
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters)
=> new JetTimeSpanTypeMapping(parameters, _options); => new JetTimeSpanTypeMapping(parameters, _options);
protected override string GenerateNonNullSqlLiteral(object value) protected override DateTime ConvertToDateTimeCompatibleValue(object value)
=> base.GenerateNonNullSqlLiteral(JetConfiguration.TimeSpanOffset + (TimeSpan) value); => JetConfiguration.TimeSpanOffset + (TimeSpan) value;
} }
} }
Loading…
Cancel
Save