@@ -26,6 +26,7 @@ namespace LinqToDB.EntityFrameworkCore
2626 using Mapping ;
2727 using Metadata ;
2828 using Extensions ;
29+ using Common . Internal . Cache ;
2930
3031 using DataProvider ;
3132 using DataProvider . DB2 ;
@@ -83,9 +84,17 @@ public override int GetHashCode()
8384
8485 readonly ConcurrentDictionary < ProviderKey , IDataProvider > _knownProviders = new ConcurrentDictionary < ProviderKey , IDataProvider > ( ) ;
8586
87+ private readonly MemoryCache _schemaCache = new MemoryCache (
88+ new MemoryCacheOptions
89+ {
90+ ExpirationScanFrequency = TimeSpan . FromHours ( 1.0 )
91+ } ) ;
92+
93+
8694 public virtual void ClearCaches ( )
8795 {
8896 _knownProviders . Clear ( ) ;
97+ _schemaCache . Compact ( 1.0 ) ;
8998 }
9099
91100 /// <summary>
@@ -348,14 +357,31 @@ public virtual IMetadataReader CreateMetadataReader(IModel model,
348357 /// <param name="model">EF.Core data model.</param>
349358 /// <param name="metadataReader">Additional optional LINQ To DB database metadata provider.</param>
350359 /// <returns>Mapping schema for provided EF.Core model.</returns>
351- public virtual MappingSchema GetMappingSchema ( IModel model , IMetadataReader metadataReader )
360+ public virtual MappingSchema CreateMappingSchema ( IModel model , IMetadataReader metadataReader )
352361 {
353362 var schema = new MappingSchema ( ) ;
354363 if ( metadataReader != null )
355364 schema . AddMetadataReader ( metadataReader ) ;
356365 return schema ;
357366 }
358367
368+ /// <summary>
369+ /// Returns mapping schema using provided EF.Core data model and metadata provider.
370+ /// </summary>
371+ /// <param name="model">EF.Core data model.</param>
372+ /// <param name="metadataReader">Additional optional LINQ To DB database metadata provider.</param>
373+ /// <returns>Mapping schema for provided EF.Core model.</returns>
374+ public virtual MappingSchema GetMappingSchema ( IModel model , IMetadataReader metadataReader )
375+ {
376+ var result = _schemaCache . GetOrCreate ( Tuple . Create ( model , metadataReader ) , e =>
377+ {
378+ e . SlidingExpiration = TimeSpan . FromHours ( 1 ) ;
379+ return CreateMappingSchema ( model , metadataReader ) ;
380+ } ) ;
381+
382+ return result ;
383+ }
384+
359385 /// <summary>
360386 /// Returns EF.Core <see cref="IDbContextOptions"/> for specific <see cref="DbContext"/> instance.
361387 /// </summary>
@@ -430,7 +456,7 @@ public static bool IsQueryable(MethodCallExpression method, bool enumerable = tr
430456 var type = method . Method . DeclaringType ;
431457
432458 return type == typeof ( Queryable ) || ( enumerable && type == typeof ( Enumerable ) ) || type == typeof ( LinqExtensions ) ||
433- type == typeof ( EntityFrameworkQueryableExtensions ) ;
459+ type == typeof ( EntityFrameworkQueryableExtensions ) ;
434460 }
435461
436462 public static object EvaluateExpression ( Expression expr )
0 commit comments