1+ using System . Reflection ;
2+
13using Microsoft . EntityFrameworkCore . Metadata ;
24
35namespace 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