66namespace SpatialFocus . EntityFrameworkCore . Extensions
77{
88 using System ;
9+ using System . Collections . Generic ;
910 using System . Linq ;
1011 using Microsoft . EntityFrameworkCore ;
1112 using Microsoft . EntityFrameworkCore . Metadata ;
@@ -14,6 +15,8 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions
1415
1516 public static class EnumLookupExtension
1617 {
18+ private static List < Type > ConcreteTypeSeededList { get ; set ; } = new List < Type > ( ) ;
19+
1720 // See https://github.com/aspnet/EntityFrameworkCore/issues/12248#issuecomment-395450990
1821 public static void ConfigureEnumLookup ( this ModelBuilder modelBuilder , EnumLookupOptions enumOptions )
1922 {
@@ -26,7 +29,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
2629 continue ;
2730 }
2831
29- if ( enumOptions . UseEnumsWithAttributesOnly && ! propertyType . GetCustomAttributes ( typeof ( EnumLookupAttribute ) , true ) . Any ( ) )
32+ if ( enumOptions . UseEnumsWithAttributesOnly && ! propertyType . HasEnumWithAttribute ( ) )
3033 {
3134 continue ;
3235 }
@@ -61,6 +64,13 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
6164 modelBuilder . Entity ( concreteType ) . Property ( keyName ) . HasConversion ( valueConverter ) ;
6265 }
6366
67+ if ( ConcreteTypeSeededList . Contains ( concreteType ) )
68+ {
69+ continue ;
70+ }
71+
72+ ConcreteTypeSeededList . Add ( concreteType ) ;
73+
6474 // TODO: Check status of https://github.com/aspnet/EntityFrameworkCore/issues/12194 before using migrations
6575 object [ ] data = Enum . GetValues ( propertyType . GetEnumOrNullableEnumType ( ) )
6676 . OfType < object > ( )
@@ -96,6 +106,24 @@ private static Type GetEnumOrNullableEnumType(this Type propertyType)
96106 return propertyType . IsEnum ? propertyType : propertyType . GetGenericArguments ( ) [ 0 ] ;
97107 }
98108
109+ private static bool HasEnumWithAttribute ( this Type propertyType )
110+ {
111+ if ( propertyType . GetCustomAttributes ( typeof ( EnumLookupAttribute ) , true ) . Any ( ) )
112+ {
113+ return true ;
114+ }
115+
116+ if ( propertyType . IsGenericType && propertyType . GetGenericTypeDefinition ( ) == typeof ( Nullable < > ) )
117+ {
118+ if ( propertyType . GetGenericArguments ( ) [ 0 ] . GetCustomAttributes ( typeof ( EnumLookupAttribute ) , true ) . Any ( ) )
119+ {
120+ return true ;
121+ }
122+ }
123+
124+ return false ;
125+ }
126+
99127 private static bool IsEnumOrNullableEnumType ( this Type propertyType )
100128 {
101129 if ( propertyType . IsEnum )
0 commit comments