Skip to content

Commit c60db31

Browse files
committed
fix problem, when enum property has nullable type.
1 parent a956b75 commit c60db31

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupExtension.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
2727
continue;
2828
}
2929

30-
if (enumOptions.UseEnumsWithAttributesOnly && !propertyType.GetCustomAttributes(typeof(EnumLookupAttribute), true).Any())
30+
if (enumOptions.UseEnumsWithAttributesOnly && !propertyType.HasEnumWithAttribute())
3131
{
3232
continue;
3333
}
@@ -43,7 +43,8 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
4343
string tableName = enumOptions.NamingFunction(typeName);
4444
enumLookupBuilder.ToTable(tableName);
4545

46-
string keyName = enumOptions.UseNumberLookup ? nameof(EnumWithNumberLookup<Enum>.Id)
46+
string keyName = enumOptions.UseNumberLookup
47+
? nameof(EnumWithNumberLookup<Enum>.Id)
4748
: nameof(EnumWithStringLookup<Enum>.Id);
4849

4950
modelBuilder.Entity(entityType.Name).HasOne(concreteType).WithMany().HasPrincipalKey(keyName).HasForeignKey(property.Name);
@@ -108,6 +109,26 @@ private static Type GetEnumOrNullableEnumType(this Type propertyType)
108109
return propertyType.IsEnum ? propertyType : propertyType.GetGenericArguments()[0];
109110
}
110111

112+
113+
private static bool HasEnumWithAttribute(this Type propertyType)
114+
{
115+
116+
if (propertyType.GetCustomAttributes(typeof(EnumLookupAttribute), true).Any())
117+
{
118+
return true;
119+
}
120+
121+
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
122+
{
123+
if (propertyType.GetGenericArguments()[0].GetCustomAttributes(typeof(EnumLookupAttribute), true).Any())
124+
{
125+
return true;
126+
}
127+
}
128+
129+
return false;
130+
}
131+
111132
private static bool IsEnumOrNullableEnumType(this Type propertyType)
112133
{
113134
if (propertyType.IsEnum)

0 commit comments

Comments
 (0)