Skip to content

Commit a181197

Browse files
author
fabien.menager
committed
Optimize navigation getters
1 parent 6b6b003 commit a181197

2 files changed

Lines changed: 15 additions & 30 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert/Graph/GraphEntityCollector.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ private void CollectEntity(object? entity, int depth)
9494

9595
foreach (var navigation in navigations)
9696
{
97-
var propertyInfo = entityType.GetProperty(navigation.PropertyName, BindingFlags.Public | BindingFlags.Instance);
98-
if (propertyInfo == null)
99-
{
100-
continue;
101-
}
102-
103-
var value = propertyInfo.GetValue(entity);
97+
var value = navigation.GetValue(entity);
10498
if (value == null)
10599
{
106100
continue;

src/PhenX.EntityFrameworkCore.BulkInsert/Metadata/NavigationMetadata.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Reflection;
2+
13
using Microsoft.EntityFrameworkCore.Metadata;
24

35
namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata;
@@ -14,6 +16,14 @@ public NavigationMetadata(INavigationBase navigation)
1416
TargetType = navigation.TargetEntityType.ClrType;
1517
IsCollection = navigation.IsCollection;
1618

19+
// Build optimized getter for the navigation property
20+
var propertyInfo = navigation.DeclaringEntityType.ClrType.GetProperty(
21+
navigation.Name,
22+
BindingFlags.Public | BindingFlags.Instance)
23+
?? throw new InvalidOperationException($"Property '{navigation.Name}' not found on type '{navigation.DeclaringEntityType.ClrType.Name}'");
24+
25+
_getter = PropertyAccessor.CreateGetter(propertyInfo);
26+
1727
if (navigation is ISkipNavigation skipNavigation)
1828
{
1929
IsManyToMany = true;
@@ -29,6 +39,8 @@ public NavigationMetadata(INavigationBase navigation)
2939
}
3040
}
3141

42+
private readonly Func<object, object?> _getter;
43+
3244
/// <summary>
3345
/// The underlying EF Core navigation.
3446
/// </summary>
@@ -75,30 +87,9 @@ public NavigationMetadata(INavigationBase navigation)
7587
public bool IsDependentToPrincipal { get; }
7688

7789
/// <summary>
78-
/// Gets the FK property names on the source entity (for dependent-to-principal navigations).
90+
/// Gets the value of the navigation property from the entity using an optimized getter.
7991
/// </summary>
80-
public IReadOnlyList<string> GetForeignKeyPropertyNames()
81-
{
82-
if (ForeignKey == null)
83-
{
84-
return [];
85-
}
86-
87-
return ForeignKey.Properties.Select(p => p.Name).ToList();
88-
}
89-
90-
/// <summary>
91-
/// Gets the principal key property names.
92-
/// </summary>
93-
public IReadOnlyList<string> GetPrincipalKeyPropertyNames()
94-
{
95-
if (ForeignKey == null)
96-
{
97-
return [];
98-
}
99-
100-
return ForeignKey.PrincipalKey.Properties.Select(p => p.Name).ToList();
101-
}
92+
public object? GetValue(object entity) => _getter.Invoke(entity);
10293

10394
public override string ToString()
10495
{

0 commit comments

Comments
 (0)