Skip to content

Commit d067e8c

Browse files
committed
Fix TryGetEntityName for custom entity names
1 parent ad9c639 commit d067e8c

File tree

5 files changed

+338
-48
lines changed

5 files changed

+338
-48
lines changed

src/NHibernate/Async/Persister/Entity/IEntityPersister.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Collections.Generic;
2121
using NHibernate.Intercept;
2222
using NHibernate.Util;
23-
using System.Linq;
2423

2524
namespace NHibernate.Persister.Entity
2625
{

src/NHibernate/Linq/Functions/ListIndexerGenerator.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@ namespace NHibernate.Linq.Functions
1212
{
1313
internal class ListIndexerGenerator : BaseHqlGeneratorForMethod,IRuntimeMethodHqlGenerator
1414
{
15+
private static readonly HashSet<MethodInfo> _supportedMethods = new HashSet<MethodInfo>
16+
{
17+
ReflectHelper.GetMethodDefinition(() => Enumerable.ElementAt<object>(null, 0)),
18+
ReflectHelper.GetMethodDefinition(() => Queryable.ElementAt<object>(null, 0))
19+
};
20+
1521
public ListIndexerGenerator()
1622
{
17-
SupportedMethods = new[]
18-
{
19-
ReflectHelper.GetMethodDefinition(() => Enumerable.ElementAt<object>(null, 0)),
20-
ReflectHelper.GetMethodDefinition(() => Queryable.ElementAt<object>(null, 0))
21-
};
23+
SupportedMethods = _supportedMethods;
2224
}
2325

2426
public bool SupportsMethod(MethodInfo method)
2527
{
26-
return method != null &&
27-
method.Name == "get_Item" &&
28-
(method.IsMethodOf(typeof(IList)) || method.IsMethodOf(typeof(IList<>)));
28+
return IsRuntimeMethodSupported(method);
29+
}
30+
31+
public static bool IsMethodSupported(MethodInfo method)
32+
{
33+
if (method.IsGenericMethod)
34+
{
35+
method = method.GetGenericMethodDefinition();
36+
}
37+
38+
return _supportedMethods.Contains(method) || IsRuntimeMethodSupported(method);
2939
}
3040

3141
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
@@ -40,5 +50,12 @@ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
4050

4151
return treeBuilder.Index(collection, index);
4252
}
53+
54+
private static bool IsRuntimeMethodSupported(MethodInfo method)
55+
{
56+
return method != null &&
57+
method.Name == "get_Item" &&
58+
(method.IsMethodOf(typeof(IList)) || method.IsMethodOf(typeof(IList<>)));
59+
}
4360
}
44-
}
61+
}

src/NHibernate/Linq/Visitors/NullableExpressionDetector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ private bool IsNullable(MemberExpression memberExpression, BinaryExpression equa
163163
}
164164

165165
// We have to check the member mapping to determine if is nullable
166-
var entityName = ExpressionsHelper.TryGetEntityName(_sessionFactory, memberExpression, out var memberPath);
167-
if (entityName == null)
166+
var entityName = ExpressionsHelper.TryGetEntityName(_sessionFactory, memberExpression, out var memberPath, out _);
167+
if (entityName == null || memberPath == null)
168168
{
169169
return true; // Not mapped
170170
}

src/NHibernate/Tuple/Entity/EntityMetamodel.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class EntityMetamodel
5151

5252
private readonly Dictionary<string, int?> propertyIndexes = new Dictionary<string, int?>();
5353
private readonly IDictionary<string, IType> _identifierPropertyTypes = new Dictionary<string, IType>();
54+
private readonly IDictionary<string, IType> _propertyTypes = new Dictionary<string, IType>();
5455
private readonly bool hasCollections;
5556
private readonly bool hasMutableProperties;
5657
private readonly bool hasLazyProperties;
@@ -418,15 +419,17 @@ private void MapPropertyToIndex(Mapping.Property prop, int i)
418419

419420
private void MapPropertyToIndex(string path, Mapping.Property prop, int i)
420421
{
421-
propertyIndexes[!string.IsNullOrEmpty(path) ? $"{path}.{prop.Name}" : prop.Name] = i;
422+
var propPath = !string.IsNullOrEmpty(path) ? $"{path}.{prop.Name}" : prop.Name;
423+
propertyIndexes[propPath] = i;
424+
_propertyTypes[propPath] = prop.Type;
422425
if (!(prop.Value is Mapping.Component comp))
423426
{
424427
return;
425428
}
426429

427430
foreach (var subprop in comp.PropertyIterator)
428431
{
429-
MapPropertyToIndex(!string.IsNullOrEmpty(path) ? $"{path}.{prop.Name}" : prop.Name, subprop, i);
432+
MapPropertyToIndex(propPath, subprop, i);
430433
}
431434
}
432435

@@ -441,7 +444,7 @@ private void MapIdentifierPropertyTypes(string path, IType propertyType)
441444
{
442445
_identifierPropertyTypes[path] = propertyType;
443446
}
444-
447+
445448
if (propertyType is IAbstractComponentType componentType)
446449
{
447450
for (var i = 0; i < componentType.PropertyNames.Length; i++)
@@ -572,6 +575,13 @@ internal IType GetIdentifierPropertyType(string memberPath)
572575
return _identifierPropertyTypes.TryGetValue(memberPath, out var propertyType) ? propertyType : null;
573576
}
574577

578+
internal IType GetPropertyType(string memberPath)
579+
{
580+
return _propertyTypes.TryGetValue(memberPath, out var propertyType)
581+
? propertyType
582+
: GetIdentifierPropertyType(memberPath);
583+
}
584+
575585
public bool HasCollections
576586
{
577587
get { return hasCollections; }

0 commit comments

Comments
 (0)