Skip to content

Commit

Permalink
Fix for issue #72
Browse files Browse the repository at this point in the history
SqlServerHierarchyIdTypeMappingSourcePlugin don't work correctly when ClrType is null
  • Loading branch information
yuryjhol authored and ajcvickers committed Jan 5, 2022
1 parent f269397 commit 0d2cb87
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
56 changes: 56 additions & 0 deletions EFCore.SqlServer.HierarchyId.Test/TypeMappingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Microsoft.EntityFrameworkCore.SqlServer.Storage;
using Microsoft.EntityFrameworkCore.Storage;
using Xunit;

namespace Microsoft.EntityFrameworkCore.SqlServer
{
public class TypeMappingTests
{
[Fact]
public void Maps_int_column()
{
var mapping = CreateMapper().FindMapping(
new RelationalTypeMappingInfo(
storeTypeName: "int",
storeTypeNameBase: "int",
unicode: null,
size: null,
precision: null,
scale: null));

Assert.Null(mapping);
}

[Fact]
public void Maps_hierarchyid_column()
{
var mapping = CreateMapper().FindMapping(
new RelationalTypeMappingInfo(
storeTypeName: SqlServerHierarchyIdTypeMappingSourcePlugin.SqlServerTypeName,
storeTypeNameBase: SqlServerHierarchyIdTypeMappingSourcePlugin.SqlServerTypeName,
unicode: null,
size: null,
precision: null,
scale: null));

AssertMapping<HierarchyId>(mapping);
}

private static void AssertMapping<T>(
RelationalTypeMapping mapping)
{
AssertMapping(typeof(T), mapping);
}

private static void AssertMapping(
Type type,
RelationalTypeMapping mapping)
{
Assert.Same(type, mapping.ClrType);
}

private static IRelationalTypeMappingSourcePlugin CreateMapper()
=> new SqlServerHierarchyIdTypeMappingSourcePlugin();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ internal class SqlServerHierarchyIdTypeMappingSourcePlugin : IRelationalTypeMapp

public virtual RelationalTypeMapping FindMapping(in RelationalTypeMappingInfo mappingInfo)
{
var clrType = mappingInfo.ClrType ?? typeof(HierarchyId);
var clrType = mappingInfo.ClrType;
var storeTypeName = mappingInfo.StoreTypeName;

return typeof(HierarchyId).IsAssignableFrom(clrType) || storeTypeName == SqlServerTypeName
? new SqlServerHierarchyIdTypeMapping(SqlServerTypeName, clrType)
? new SqlServerHierarchyIdTypeMapping(SqlServerTypeName, clrType ?? typeof(HierarchyId))
: null;
}
}
Expand Down

0 comments on commit 0d2cb87

Please sign in to comment.