@@ -38,9 +38,26 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
3838
3939 IMutableEntityType entityType = property . DeclaringEntityType ;
4040
41- Type concreteType = enumOptions . UseNumberLookup
42- ? typeof ( EnumWithNumberLookup < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) )
43- : typeof ( EnumWithStringLookup < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) ) ;
41+ Dictionary < int , string > enumValueDescriptions = Enum . GetValues ( propertyType . GetEnumOrNullableEnumType ( ) )
42+ . Cast < Enum > ( )
43+ . ToDictionary ( Convert . ToInt32 , GetEnumDescription ) ;
44+
45+ bool usesDescription = enumValueDescriptions . Values . Any ( x => x != null ) ;
46+
47+ Type concreteType ;
48+ if ( usesDescription )
49+ {
50+ concreteType = enumOptions . UseNumberLookup
51+ ? typeof ( EnumWithNumberLookupAndDescription < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) )
52+ : typeof ( EnumWithStringLookupAndDescription < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) ) ;
53+ }
54+ else
55+ {
56+ concreteType = enumOptions . UseNumberLookup
57+ ? typeof ( EnumWithNumberLookup < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) )
58+ : typeof ( EnumWithStringLookup < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) ) ;
59+ }
60+
4461 EntityTypeBuilder enumLookupBuilder = modelBuilder . Entity ( concreteType ) ;
4562
4663 string typeName = propertyType . GetEnumOrNullableEnumType ( ) . Name ;
@@ -56,8 +73,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
5673 {
5774 modelBuilder . Entity ( concreteType ) . HasIndex ( nameof ( EnumWithNumberLookup < Enum > . Name ) ) . IsUnique ( ) ;
5875 }
59-
60- if ( ! enumOptions . UseNumberLookup )
76+ else
6177 {
6278 Type converterType = typeof ( EnumToStringConverter < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) ) ;
6379 ValueConverter valueConverter = ( ValueConverter ) Activator . CreateInstance ( converterType , new object [ ] { null } ) ;
@@ -73,10 +89,6 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
7389
7490 ConcreteTypeSeededList . Add ( concreteType ) ;
7591
76- Dictionary < int , string > enumValueDescriptions = Enum . GetValues ( propertyType . GetEnumOrNullableEnumType ( ) )
77- . Cast < Enum > ( )
78- . ToDictionary ( Convert . ToInt32 , GetEnumDescription ) ;
79-
8092 // TODO: Check status of https://github.com/aspnet/EntityFrameworkCore/issues/12194 before using migrations
8193 object [ ] data = Enum . GetValues ( propertyType . GetEnumOrNullableEnumType ( ) )
8294 . OfType < object > ( )
@@ -88,14 +100,22 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
88100 {
89101 concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Id ) ) . SetValue ( instance , x ) ;
90102 concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Name ) ) . SetValue ( instance , x . ToString ( ) ) ;
91- concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Description ) )
92- . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
103+
104+ if ( usesDescription )
105+ {
106+ concreteType . GetProperty ( nameof ( EnumWithNumberLookupAndDescription < object > . Description ) )
107+ . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
108+ }
93109 }
94110 else
95111 {
96112 concreteType . GetProperty ( nameof ( EnumWithStringLookup < object > . Id ) ) . SetValue ( instance , x ) ;
97- concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Description ) )
98- . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
113+
114+ if ( usesDescription )
115+ {
116+ concreteType . GetProperty ( nameof ( EnumWithNumberLookupAndDescription < object > . Description ) )
117+ . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
118+ }
99119 }
100120
101121 return instance ;
@@ -108,9 +128,9 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
108128
109129 public static string GetEnumDescription ( Enum value )
110130 {
111- FieldInfo fi = value . GetType ( ) . GetField ( value . ToString ( ) ) ;
131+ FieldInfo fieldInfo = value . GetType ( ) . GetField ( value . ToString ( ) ) ;
112132
113- DescriptionAttribute attribute = ( DescriptionAttribute ) fi . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
133+ DescriptionAttribute attribute = ( DescriptionAttribute ) fieldInfo . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
114134
115135 return attribute ? . Description ;
116136 }
0 commit comments