@@ -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 ;
@@ -71,6 +73,10 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
7173
7274 ConcreteTypeSeededList . Add ( concreteType ) ;
7375
76+ Dictionary < int , string > enumValueDescriptions = Enum . GetValues ( propertyType . GetEnumOrNullableEnumType ( ) )
77+ . Cast < Enum > ( )
78+ . ToDictionary ( Convert . ToInt32 , GetEnumDescription ) ;
79+
7480 // TODO: Check status of https://github.com/aspnet/EntityFrameworkCore/issues/12194 before using migrations
7581 object [ ] data = Enum . GetValues ( propertyType . GetEnumOrNullableEnumType ( ) )
7682 . OfType < object > ( )
@@ -82,10 +88,14 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
8288 {
8389 concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Id ) ) . SetValue ( instance , x ) ;
8490 concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Name ) ) . SetValue ( instance , x . ToString ( ) ) ;
91+ concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Description ) )
92+ . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
8593 }
8694 else
8795 {
8896 concreteType . GetProperty ( nameof ( EnumWithStringLookup < object > . Id ) ) . SetValue ( instance , x ) ;
97+ concreteType . GetProperty ( nameof ( EnumWithNumberLookup < object > . Description ) )
98+ . SetValue ( instance , enumValueDescriptions [ ( int ) x ] ) ;
8999 }
90100
91101 return instance ;
@@ -96,6 +106,15 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
96106 }
97107 }
98108
109+ public static string GetEnumDescription ( Enum value )
110+ {
111+ FieldInfo fi = value . GetType ( ) . GetField ( value . ToString ( ) ) ;
112+
113+ DescriptionAttribute attribute = ( DescriptionAttribute ) fi . GetCustomAttribute ( typeof ( DescriptionAttribute ) , true ) ;
114+
115+ return attribute ? . Description ;
116+ }
117+
99118 private static Type GetEnumOrNullableEnumType ( this Type propertyType )
100119 {
101120 if ( ! propertyType . IsEnumOrNullableEnumType ( ) )
0 commit comments