diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj index d3b8ebe3..b73e89b7 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdmin.DataAccess.PetaPoco.csproj @@ -4,7 +4,7 @@ - + diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs index 0bbaa2da..429c737f 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/BootstrapAdminConventionMapper.cs @@ -1,5 +1,4 @@ -using BootstrapAdmin.DataAccess.PetaPoco.Coverters; -using PetaPoco; +using PetaPoco; using System.Reflection; namespace BootstrapAdmin.DataAccess.PetaPoco; @@ -65,10 +64,10 @@ class BootstrapAdminConventionMapper : ConventionMapper } public override Func GetFromDbConverter(PropertyInfo targetProperty, Type sourceType) => targetProperty.PropertyType.IsEnum && sourceType == typeof(string) - ? new StringToEnumConverter(targetProperty.PropertyType).ConvertFromDb + ? new NumberToEnumConverter(targetProperty.PropertyType).ConvertFromDb : base.GetFromDbConverter(targetProperty, sourceType); public override Func GetToDbConverter(PropertyInfo targetProperty) => targetProperty.PropertyType.IsEnum - ? new StringToEnumConverter(targetProperty.PropertyType).ConvertToDb + ? new NumberToEnumConverter(targetProperty.PropertyType).ConvertToDb : base.GetToDbConverter(targetProperty); } diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs deleted file mode 100644 index 437ce0a5..00000000 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Coverters/StringToEnumConverter.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace BootstrapAdmin.DataAccess.PetaPoco.Coverters; - -/// -/// 字符串转枚举转换器 -/// -class StringToEnumConverter -{ - private Type TargetType { get; set; } - - /// - /// - /// - /// - public StringToEnumConverter(Type targetType) => TargetType = targetType; - - /// - /// - /// - /// - /// - /// - public object? ConvertFromDb(object? value) - { - if (value == null) - { - throw new ArgumentNullException(nameof(value)); - } - - object? ret; - if (value != null && Enum.TryParse(TargetType, value.ToString(), true, out var v)) - { - ret = v; - } - else - { - throw new InvalidCastException($"{value} 无法转化为 {TargetType.Name} 成员"); - } - return ret; - } - - /// - /// - /// - /// - /// - /// - public object? ConvertToDb(object? value) - { - object? ret; - var field = value?.ToString(); - if (!string.IsNullOrEmpty(field) && Enum.TryParse(TargetType, field, out var v)) - { - ret = v; - } - else - { - throw new InvalidCastException($"{TargetType.Name} 未定义 {field} 成员"); - } - return ret; - } -}