AddSpaces
public abstract class EnumDbEntity<TEnum> where TEnum : struct, IConvertible { [Required] public TEnum Id { get; set; } [Required, StringLength(100)] public string DisplayName { get; set; } /// <summary> /// TODO: Override this method where needed, especially if the derived class has extra fields. /// </summary> /// <param name="value"></param> /// <param name="addSpaces"></param> /// <returns></returns> public virtual EnumDbEntity<TEnum> InitForSeeding(TEnum value, bool addSpaces = true) { Id = value; DisplayName = EnumHelper<TEnum>.GetDisplayValue(value, addSpaces); return this; } } public static class EnumDbEntityExtensions { public static void SeedEnumTable<TEnum, TEntity>(this IDbSet<TEntity> dbSet, bool addSpaces = true) where TEnum : struct, IConvertible where TEntity : EnumDbEntity<TEnum>, new() { if (!typeof(TEnum).IsEnum) throw new ArgumentException("TEnum must be an enumerated type"); var itemValues = (TEnum[])Enum.GetValues(typeof(TEnum)); foreach (var itemValue in itemValues) { var item = new TEntity(); item.InitForSeeding(itemValue, addSpaces); dbSet.AddOrUpdate(a => a.Id, item); } } } /* Reference * https://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code */ public static class EnumHelper<T> where T : struct, IConvertible { public static string GetDisplayValue(T value, bool addSpaces = true) { var ret = String.Empty; var fieldInfo = value.GetType().GetField(value.ToString()); if (fieldInfo == null) { return "Display Name Unknown"; } var dispAttrib = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[]; if (dispAttrib != null) { if (dispAttrib.Length > 0) { ret = dispAttrib[0].Name; } else { ret = value.ToString(); if (addSpaces) { ret = ret.AddSpaces(); } } } return ret; } }
public enum OrderStatusEnum { Active, Inactive } public class OrderStatus : EnumDbEntity<OrderStatusEnum> { }
context.OrderStatuses.SeedEnumTable<OrderStatusEnum, OrderStatus>();
using Microsoft.EntityFrameworkCore; using NetSoft.iPathPro.Common; using System; using System.ComponentModel.DataAnnotations; public abstract class EnumDbEntity<TEnum> where TEnum : struct, IConvertible { [Required] public TEnum Id { get; set; } [Required, StringLength(100)] public string DisplayName { get; set; } /// <summary> /// TODO: Override this method where needed, especially if the derived class has extra fields. /// </summary> /// <param name="value"></param> /// <param name="addSpaces"></param> /// <returns></returns> public virtual EnumDbEntity<TEnum> InitForSeeding(TEnum value, bool addSpaces = true) { Id = value; DisplayName = EnumHelper<TEnum>.GetDisplayValue(value, addSpaces); return this; } } public static class EnumDbEntityExtensions { /*--- EF Core Version ---*/ /* Usage: within DbContext's "protected override void OnModelCreating(ModelBuilder mb)" add the */ /* following code: mb.SeedEnumTable<TEnum, TEntity>(); */ /* Be sure to subsitute your enum and entity class for TEnum and TEntity, respectively. */ public static void SeedEnumTable<TEnum, TEntity>(this ModelBuilder mb, bool addSpaces = true) where TEnum : struct, IConvertible where TEntity : EnumDbEntity<TEnum>, new() { if (!typeof(TEnum).IsEnum) throw new ArgumentException("TEnum must be an enumerated type"); var itemValues = (TEnum[])Enum.GetValues(typeof(TEnum)); foreach (var itemValue in itemValues) { var item = new TEntity(); item.InitForSeeding(itemValue, addSpaces); mb.Entity<TEntity>().HasData(item); } } private static void SeedEnumTable<TEnum, TEntity>(ModelBuilder mb, TEnum pEnum, TEntity pEntity, bool addSpaces = true) where TEnum : struct, IConvertible where TEntity : EnumDbEntity<TEnum>, new() { mb.SeedEnumTable<TEnum, TEntity>(addSpaces); } public static void SeedAllEnumTables( this ModelBuilder mb, IEnumerable<Type> excludedTypes, bool addSpaces = true) { /*--- Get DAL Assembly ---*/ var asm = Assembly.GetExecutingAssembly(); var types = asm.DefinedTypes .Where(x => x.BaseType != null && x.BaseType.IsGenericType && x.BaseType.Name.StartsWith("EnumDbEntity") && !excludedTypes.Contains(x) ) .OrderBy(x => x.Name) .ToList(); /*--- Loop Through all Types based on EnumDbEntity<TEnum> ---*/ foreach (var type in types) { /*--- Get Instance of any Enum value ---*/ var enumType = type.BaseType.GenericTypeArguments[0]; var enumNames = Enum.GetNames(enumType); /*--- Skip Empty Enums ---*/ if (enumNames.Length == 0) { continue; } dynamic pEnum = Enum.Parse(enumType, enumNames[0]); /*--- Create Instance of EntityType ---*/ dynamic pEntity = Activator.CreateInstance(type); /*--- Seed the Table ---*/ SeedEnumTable(mb, pEnum, pEntity, addSpaces); } } } /* Reference * https://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code */ public static class EnumHelper<T> where T : struct, IConvertible { public static string GetDisplayValue(T value, bool addSpaces = true) { var ret = String.Empty; var fieldInfo = value.GetType().GetField(value.ToString()); if (fieldInfo == null) { return "Display Name Unknown"; } var dispAttrib = fieldInfo.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[]; if (dispAttrib != null) { if (dispAttrib.Length > 0) { ret = dispAttrib[0].Name; } else { ret = value.ToString(); if (addSpaces) { ret = ret.AddSpaces(); } } } return ret; } }
protected override void OnModelCreating(ModelBuilder mb) { // TODO - Repeat the following line for each EnumDbEntity implementation mb.SeedEnumTable<OrderStatusEnum, OrderStatus>(); }
protected override void OnModelCreating(ModelBuilder mb) { // The next line seed every database table where the entity inherits from EnumDbEntity<TEnum> // EXCEPT for those types listed in the "excludedTypes" array. These types are excluded because // they require special handling. mb.SeedAllEnumTables(new[] { // TODO - Repeat the following line for each special case typeof(MyEntity) }); // TODO - Repeat the following line for each special case mb.SeedEnumTable<MyEntityEnum, MyEntity>(false); }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.