11using System ;
22using System . Collections . Generic ;
33using System . Diagnostics . CodeAnalysis ;
4+ using System . Linq ;
45using System . Threading ;
56using Npgsql . Internal ;
67using Npgsql . Internal . Postgres ;
@@ -15,6 +16,8 @@ sealed class GlobalTypeMapper : INpgsqlTypeMapper
1516 readonly ReaderWriterLockSlim _lock = new ( ) ;
1617 IPgTypeInfoResolver [ ] _typeMappingResolvers = Array . Empty < IPgTypeInfoResolver > ( ) ;
1718
19+ internal List < HackyEnumTypeMapping > HackyEnumTypeMappings { get ; } = new ( ) ;
20+
1821 internal IEnumerable < IPgTypeInfoResolver > GetPluginResolvers ( )
1922 {
2023 var resolvers = new List < IPgTypeInfoResolver > ( ) ;
@@ -154,6 +157,7 @@ public void Reset()
154157 {
155158 _pluginResolvers . Clear ( ) ;
156159 _userTypeMapper . Items . Clear ( ) ;
160+ HackyEnumTypeMappings . Clear ( ) ;
157161 }
158162 finally
159163 {
@@ -175,6 +179,11 @@ public INpgsqlTypeMapper MapEnum<TEnum>(string? pgName = null, INpgsqlNameTransl
175179 try
176180 {
177181 _userTypeMapper . MapEnum < TEnum > ( pgName , nameTranslator ) ;
182+
183+ // Temporary hack for EFCore.PG enum mapping compat
184+ if ( _userTypeMapper . Items . FirstOrDefault ( i => i . ClrType == typeof ( TEnum ) ) is UserTypeMapping userTypeMapping )
185+ HackyEnumTypeMappings . Add ( new ( typeof ( TEnum ) , userTypeMapping . PgTypeName , nameTranslator ?? DefaultNameTranslator ) ) ;
186+
178187 return this ;
179188 }
180189 finally
@@ -189,7 +198,13 @@ public bool UnmapEnum<TEnum>(string? pgName = null, INpgsqlNameTranslator? nameT
189198 _lock . EnterWriteLock ( ) ;
190199 try
191200 {
192- return _userTypeMapper . UnmapEnum < TEnum > ( pgName , nameTranslator ) ;
201+ var removed = _userTypeMapper . UnmapEnum < TEnum > ( pgName , nameTranslator ) ;
202+
203+ // Temporary hack for EFCore.PG enum mapping compat
204+ if ( removed && ( ( List < UserTypeMapping > ) _userTypeMapper . Items ) . FindIndex ( m => m . ClrType == typeof ( TEnum ) ) is > - 1 and var index )
205+ HackyEnumTypeMappings . RemoveAt ( index ) ;
206+
207+ return removed ;
193208 }
194209 finally
195210 {
0 commit comments