Skip to content

Commit 934af2a

Browse files
authored
Merge pull request #5 from cioxideru/master
try to fix problem with many fields with same enum type
2 parents 4dec17a + 6c500f8 commit 934af2a

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupExtension.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace 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

Comments
 (0)