@@ -7,41 +7,39 @@ namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata;
77
88internal sealed class MetadataProvider
99{
10- private Dictionary < Type , Dictionary < Type , TableMetadata > > _tablesPerContext = new ( ) ;
10+ private readonly Dictionary < Type , Dictionary < Type , TableMetadata > > _tablesPerContext = new ( ) ;
1111
1212 public TableMetadata GetTableInfo < T > ( DbContext context )
13- {
14- var tables = GetTables ( context ) ;
15-
16- if ( ! tables . TryGetValue ( typeof ( T ) , out var table ) )
17- {
18- throw new InvalidOperationException ( $ "Cannot find metadata for type '{ typeof ( T ) } '.") ;
19- }
20-
21- return table ;
22- }
23-
24- private Dictionary < Type , TableMetadata > GetTables ( DbContext context )
2513 {
2614 lock ( _tablesPerContext )
2715 {
2816 var type = context . GetType ( ) ;
29- if ( _tablesPerContext . TryGetValue ( context . GetType ( ) , out var tables ) )
17+
18+ if ( ! _tablesPerContext . TryGetValue ( type , out var tables ) )
3019 {
31- return tables ;
20+ tables = new Dictionary < Type , TableMetadata > ( ) ;
21+ _tablesPerContext [ type ] = tables ;
3222 }
3323
34- var provider = context . GetService < IBulkInsertProvider > ( ) ;
24+ var modelType = typeof ( T ) ;
25+
26+ if ( tables . TryGetValue ( modelType , out var table ) )
27+ {
28+ return table ;
29+ }
3530
36- tables = context . Model . GetEntityTypes ( )
37- . GroupBy ( x => x . ClrType )
38- . ToDictionary (
39- x => x . Key ,
40- x => new TableMetadata ( x . First ( ) , provider . SqlDialect ) ) ;
31+ var entityType = context . Model . FindEntityType ( modelType ) ;
32+ if ( entityType == null )
33+ {
34+ throw new InvalidOperationException ( $ "The type '{ modelType . FullName } ' is not part of the model for the current context.") ;
35+ }
36+
37+ var provider = context . GetService < IBulkInsertProvider > ( ) ;
4138
42- _tablesPerContext [ type ] = tables ;
39+ var tableMetadata = new TableMetadata ( entityType , provider . SqlDialect ) ;
40+ tables [ modelType ] = tableMetadata ;
4341
44- return tables ;
42+ return tableMetadata ;
4543 }
4644 }
4745}
0 commit comments