EF is mass updating my entire object. Its even going so far to update things incorrectly. I’m getting my enum value set to ‘Male’ when it should be ‘M’…
public enum AvatarGender
{
[Description("M")]
Male = 0,
[Description("F")]
Female = 1
}
I’ve literally told EF it has a conversion:
modelBuilder.Entity<PlayerAvatarData>()
.Property(e => e.Gender)
.HasConversion(
v => v.ToString(),
v => EnumHelpers.GetEnumValueFromDescription<AvatarGender>(v));
What more do I need to tell the framework to use M?
(Time of AI, AI better, Computers know better, Humans suck, AI better, AI knows better, EF superior loooool).
It happens when saving data like:
player.AvatarData.Motto = newMotto;
dbContext.PlayerAvatarData.Update(player.AvatarData);
await dbContext.SaveChangesAsync();
GetEnumValueFromDescription
public static T GetEnumValueFromDescription<T>(string description)
{
var fis = typeof(T).GetFields();
foreach (var fi in fis)
{
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0 && attributes[0].Description == description)
{
return (T)Enum.Parse(typeof(T), fi.Name);
}
}
throw new Exception($"Failed to resolve enum from description '{description}'");
}
adam says is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8