Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch update ignore custom type conversion (Npgsql) #4

Closed
heggi opened this issue Dec 29, 2020 · 3 comments
Closed

Batch update ignore custom type conversion (Npgsql) #4

heggi opened this issue Dec 29, 2020 · 3 comments

Comments

@heggi
Copy link

heggi commented Dec 29, 2020

I use custom converter for convert from enum to string and back (storing enum in DB as string)

public class CustomEnumConverter<TEnum> : ValueConverter<TEnum, string> where TEnum : struct, Enum
{
    public CustomEnumConverter(ConverterMappingHints mappingHints = null)
        : base(toStringExpr, toEnumExpr, mappingHints) { }

    private readonly static Expression<Func<TEnum, string>> toStringExpr = x => ToDbString(x);
    private readonly static Expression<Func<string, TEnum>> toEnumExpr = x => ToEnum(x);

    public static string ToDbString(TEnum tEnum)
    {
        var enumType = tEnum.GetType();
        var memInfo = enumType.GetMember(tEnum.ToString());

        var attr = (EnumMemberAttribute)memInfo[0]
            .GetCustomAttributes(typeof(EnumMemberAttribute), false)
            .FirstOrDefault();

        return attr.Value;
    }

    public static TEnum ToEnum(string value)
    {
        foreach (var name in Enum.GetNames<TEnum>()) {
            var attr = typeof(TEnum)
                .GetRuntimeField(name)
                .GetCustomAttribute<EnumMemberAttribute>(true);
            if (attr != null && attr.Value == value) {
                return Enum.Parse<TEnum>(name);
            }
        }

        return default;
    }
}

When I use standard EFCore functions (such as context.Sess.Add(entity)), all works fine (enums stored as string),
but batchUpdate save to DB int value of enum member, not string.

    db.BatchUpdate<Entities.Sess>()
          .Set(m => m.Status, m => Enums.SessStatus.Stopreq) // '1' in DB, must be 'stopreq'
          .Execute()

My Enum and Entity

public enum SessStatus
{
    [EnumMember(Value = "active")]
    Active,

    [EnumMember(Value = "stopreq")]
    Stopreq,

    [EnumMember(Value = "stopping")]
    Stopping,
}

public class Sess
{
    ...
    public SessStatus Status { get; set; }
    ...
}

public class AppDbContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .Entity<Sess>()
            .Property(m => m.Status)
            .HasConversion(new Converters.CustomEnumConverter<SessStatus>());

        base.OnModelCreating(modelBuilder);
    }
}
@yangzhongke
Copy link
Owner

Thanks for you feedback, and I will check it later.

yangzhongke added a commit that referenced this issue Dec 30, 2020
@yangzhongke
Copy link
Owner

Bug fixed, please update to the latest NuGet package.
Please let me know if it solves your issue.

@yangzhongke
Copy link
Owner

hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants