要在 Entity Framework 中播种类型表,我想要一个采用枚举并创建类型对象列表和 Name 属性的通用方法

To seed type tables in Entity Framework I want a generic method that takes an enum and creates a list of type objects and Name properties

为了在 Entity Framework 核心中为类型 tables 提供种子,我正在为每个需要种子的数据库类型 table 在函数 'StopLightColorTypeList' 中重写相同的代码。我想创建一个通用方法。

    modelBuilder.Entity<StopLightColorType> 
       ().HasData(WLProgramSeed.StopLightColorTypeList());

StopLightColorTypeList 为每个枚举成员创建一个新的 StopLightColorType 对象并将其添加到列表之前:

将对象 ID 属性 设置为枚举的 int 值 将名称 属性 设置为枚举

的字符串值

我希望尽可能地使用泛型

using System;
using System.Collections.Generic;
using System.Linq;

namespace GenericEnumToTypeList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<StopLightColorType> stopLightColorTypes = EFTools.StopLightColorTypes();

            foreach(var stopLightColorType in stopLightColorTypes)
            {
                Console.WriteLine("ID: {0} Name: {1}", stopLightColorType.StopLightColorID, stopLightColorType.StopLightColorName);
            }

            Console.ReadLine();
        }
    }

    public enum StopLightColorsEnum
    {
        Red = 1,
        Yellow = 2,
        Green = 3
    }

    public class StopLightColorType
    {
        public StopLightColorsEnum StopLightColorID { get; set; }
        public string StopLightColorName { get; set; }
    }

    public class EFTools
    {

        public static List<StopLightColorType> StopLightColorTypes()
        {
            List<StopLightColorType> stopLightColorTypes = new List<StopLightColorType>();

            foreach(StopLightColorsEnum stopLightColor in System.Enum.GetValues(typeof(StopLightColorsEnum))
            {
                StopLightColorType stopLightColorType  = new StopLightColorType
                {
                    StopLightColorID = stopLightColor,
                    StopLightColorName = ParseCapitalizedEnumName(stopLightColor.ToString())
                };

                stopLightColorTypes.Add(stopLightColorType);
            }

            return stopLightColorTypes;
        }

        public static string ParseUnderScoreName(string stringName)
        {            
            return stringName.Replace("_", " "); ;
        }

        public static string ParseCapitalizedEnumName(string enumName)
        {

            string typeName = enumName[0].ToString();

            for(int i = 1; i <= enumName.Length; i++)
            {
                if(Char.ToUpper(enumName[i]) == enumName[i])
                {
                    typeName = typeName + " " + enumName[i].ToString(); 
                }
            }

            return typeName;
        }

    }

}

我为每种数据库类型重新编写了这段代码,我正在寻找一个泛型来处理这个操作。

你需要的核心功能是这个:

IEnumerable<TResult> ListEnumMembers<TEnum, TResult>(string valueName, string nameName)
    where TEnum : System.Enum
{
    var enumInfos = Enum.GetValues(typeof(TEnum)).Cast<int>()
        .Zip(Enum.GetNames(typeof(TEnum)), (i, s) => (Value: i, Name: s));
    
    var typ = typeof(TResult);
    var piValue = typ.GetProperty(valueName);
    var piName = typ.GetProperty(nameName);
    return enumInfos.Select(i =>
    {
        var instance = (TResult)Activator.CreateInstance(typ);
        piValue.SetValue(instance, i.Value);
        piName.SetValue(instance, i.Name);
        return instance;
    });
}

有了这个通用结果类型...

class EnumInfo
{
    public int Value { get; set; }
    public string Name { get; set; }
}

...以及此代码:

var enumInfos = ListEnumMembers<DayOfWeek, EnumInfo>("Value", "Name").ToList();

你得到这个结果:

Value Name
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday

我想这会让您走上正轨。当然,代码需要对 属性 名称和类型进行一些检查。