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

Allow type mapping source plugins to override built-in mappings #24662

Merged
1 commit merged into from
Apr 14, 2021
Merged
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 @@ -247,8 +247,7 @@ public SqlServerTypeMappingSource(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
=> FindRawMapping(mappingInfo)?.Clone(mappingInfo)
?? base.FindMapping(mappingInfo);
=> base.FindMapping(mappingInfo) ?? FindRawMapping(mappingInfo)?.Clone(mappingInfo);

private RelationalTypeMapping? FindRawMapping(RelationalTypeMappingInfo mappingInfo)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static bool IsSpatialiteType(string columnType)
/// </summary>
protected override RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
{
var mapping = FindRawMapping(mappingInfo);
var mapping = base.FindMapping(mappingInfo) ?? FindRawMapping(mappingInfo);

return mapping != null
&& mappingInfo.StoreTypeName != null
Expand All @@ -156,10 +156,7 @@ public static bool IsSpatialiteType(string columnType)
return mapping;
}

mapping = base.FindMapping(mappingInfo);

if (mapping == null
&& storeTypeName != null)
if (storeTypeName != null)
{
var affinityTypeMapping = _typeRules.Select(r => r(storeTypeName)).FirstOrDefault(r => r != null);

Expand All @@ -175,7 +172,7 @@ public static bool IsSpatialiteType(string columnType)
}
}

return mapping;
return null;
}

private readonly Func<string, RelationalTypeMapping?>[] _typeRules =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,6 @@ public void Does_key_SQL_Server_string_mapping(bool? unicode, bool? fixedLength)
Assert.Equal(450, typeMapping.CreateParameter(new TestCommand(), "Name", "Value").Size);
}

private static IRelationalTypeMappingSource CreateTypeMapper()
=> new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>());

[ConditionalTheory]
[InlineData(true, false)]
[InlineData(null, false)]
Expand Down Expand Up @@ -1259,6 +1254,30 @@ public void String_FK_unicode_is_preferred_if_specified()
mapper.GetMapping(model.FindEntityType(typeof(MyRelatedType4)).FindProperty("Relationship2Id")).StoreType);
}

[ConditionalFact]
public void Plugins_can_override_builtin_mappings()
{
var typeMappingSource = new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>() with
{
Plugins = new[] { new FakeTypeMappingSourcePlugin() }
});

Assert.Equal("String", typeMappingSource.GetMapping("datetime2").ClrType.Name);
}

class FakeTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
{
public RelationalTypeMapping FindMapping(in RelationalTypeMappingInfo mappingInfo)
=> new StringTypeMapping("datetime2");
}

private static IRelationalTypeMappingSource CreateTypeMapper()
=> new SqlServerTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>());

private enum LongEnum : long
{
}
Expand Down