22using Microsoft . EntityFrameworkCore ;
33using Microsoft . EntityFrameworkCore . Metadata ;
44using Microsoft . EntityFrameworkCore . Storage ;
5+ using Microsoft . EntityFrameworkCore . Storage . ValueConversion ;
56using System ;
67using System . Collections . Concurrent ;
78using System . Collections . Generic ;
@@ -24,6 +25,7 @@ public static class DbContextExtensions
2425 private static readonly ConcurrentDictionary < CacheKey , IReadOnlyList < string > > _insertablePropertyNamesCache = [ ] ;
2526 private static readonly ConcurrentDictionary < CacheKey , IReadOnlyList < string > > _allPropertyNamesCache = [ ] ;
2627 private static readonly ConcurrentDictionary < CacheKey , IReadOnlyList < string > > _allPropertyNamesWithoutRowVersionsCache = [ ] ;
28+ private static readonly ConcurrentDictionary < CacheKey , IReadOnlyDictionary < string , ValueConverter > > _valueConvertersCache = [ ] ;
2729
2830 public static TableInfor GetTableInfor ( this DbContext dbContext , Type type )
2931 {
@@ -77,7 +79,8 @@ public static IReadOnlyList<ColumnInfor> GetProperties(this DbContext dbContext,
7779 ValueGenerated = entityProp . ValueGenerated ,
7880 DefaultValueSql = entityProp . GetDefaultValueSql ( ) ,
7981 IsPrimaryKey = entityProp . IsPrimaryKey ( ) ,
80- IsRowVersion = entityProp . IsRowVersion ( )
82+ IsRowVersion = entityProp . IsRowVersion ( ) ,
83+ ValueConverter = entityProp . GetValueConverter ( ) ,
8184 } ) . ToArray ( ) ;
8285 return data ;
8386 } ) ;
@@ -153,6 +156,16 @@ public static IReadOnlyList<string> GetAllPropertyNamesWithoutRowVersions(this D
153156 } ) ;
154157 }
155158
159+ public static IReadOnlyDictionary < string , ValueConverter > GetValueConverters ( this DbContext dbContext , Type type )
160+ {
161+ var cacheKey = new CacheKey ( dbContext . GetType ( ) , type ) ;
162+ return _valueConvertersCache . GetOrAdd ( cacheKey , ( key ) =>
163+ {
164+ var properties = dbContext . GetProperties ( key . EntityType ) ;
165+ return properties . Where ( x => x . ValueConverter != null ) . ToDictionary ( x => x . PropertyName , x => x . ValueConverter ) ;
166+ } ) ;
167+ }
168+
156169 public static IDbCommand CreateTextCommand ( this DbContext dbContext , string commandText , BulkOptions options = null )
157170 {
158171 return dbContext . GetSqlConnection ( ) . CreateTextCommand ( dbContext . GetCurrentSqlTransaction ( ) , commandText , options ) ;
0 commit comments