@@ -7,7 +7,9 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions
77{
88 using System ;
99 using System . Collections . Generic ;
10+ using System . ComponentModel ;
1011 using System . Linq ;
12+ using System . Reflection ;
1113 using Microsoft . EntityFrameworkCore ;
1214 using Microsoft . EntityFrameworkCore . Metadata ;
1315 using Microsoft . EntityFrameworkCore . Metadata . Builders ;
@@ -36,9 +38,26 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
3638
3739 IMutableEntityType entityType = property . DeclaringEntityType ;
3840
39- Type concreteType = enumOptions . UseNumberLookup
40- ? typeof ( EnumWithNumberLookup < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) )
41- : 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+
4261 EntityTypeBuilder enumLookupBuilder = modelBuilder . Entity ( concreteType ) ;
4362
4463 string typeName = propertyType . GetEnumOrNullableEnumType ( ) . Name ;
@@ -54,8 +73,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
5473 {
5574 modelBuilder . Entity ( concreteType ) . HasIndex ( nameof ( EnumWithNumberLookup < Enum > . Name ) ) . IsUnique ( ) ;
5675 }
57-
58- if ( ! enumOptions . UseNumberLookup )
76+ else
5977 {
6078 Type converterType = typeof ( EnumToStringConverter < > ) . MakeGenericType ( propertyType . GetEnumOrNullableEnumType ( ) ) ;
6179 ValueConverter valueConverter = ( ValueConverter ) Activator . CreateInstance ( converterType , new object [ ] { null } ) ;
@@ -82,10 +100,22 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
82100 {
83101 concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Id ) ) . SetValue ( instance , x ) ;
84102 concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Name ) ) . SetValue ( instance , x . ToString ( ) ) ;
103+
104+ if ( usesDescription )
105+ {
106+ concreteType . GetProperty ( nameof ( EnumWithNumberLookupAndDescription < object > . Description ) )
107+ . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
108+ }
85109 }
86110 else
87111 {
88112 concreteType . GetProperty ( nameof ( EnumWithStringLookup < object > . Id ) ) . SetValue ( instance , x ) ;
113+
114+ if ( usesDescription )
115+ {
116+ concreteType . GetProperty ( nameof ( EnumWithNumberLookupAndDescription < object > . Description ) )
117+ . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
118+ }
89119 }
90120
91121 return instance ;
@@ -96,6 +126,15 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
96126 }
97127 }
98128
129+ public static string GetEnumDescription ( Enum value )
130+ {
131+ FieldInfo fieldInfo = value . GetType ( ) . GetField ( value . ToString ( ) ) ;
132+
133+ DescriptionAttribute attribute = ( DescriptionAttribute ) fieldInfo . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
134+
135+ return attribute ? . Description ;
136+ }
137+
99138 private static Type GetEnumOrNullableEnumType ( this Type propertyType )
100139 {
101140 if ( ! propertyType . IsEnumOrNullableEnumType ( ) )
0 commit comments