You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that automapper only looks at the defined enum value but ignores [EnumMember(Value = "..")] attribute.
class Test
{
public string Value { get; set; }
}
class TestDto
{
public TestEnum Value { get; set; }
}
public enum TestEnum
{
[EnumMember(Value = "some-value")]
SomeValue
}
Mapper.CreateMap<Test, TestDto>();
var test = new Test { Value = "some-value };
// fails: ArgumentException: Requested value 'some-value' was not found.
var testDto = Mapper.Map<TestDto>(test);
var test = new Test { Value = "SomeValue };
// works
var testDto = Mapper.Map<TestDto>(test);
When converting the testDto to JSON it all works fine and displays the EnumMember's value.
I'm not huge fan of this, but I'm working on an ASP.NET Core application which uses an legacy database and the values there are stored with dashes, so it'd be nice if AutoMapper respect EnumMember too. Using strings here isn't an option.
The text was updated successfully, but these errors were encountered:
Do you also need the other side, enum->string?
Because as far as I see this is a bit more involved, as currently this way is done via the default StringMapper.cs and I don't think the handling of that Enum specific attribute has a real place in there, so probably a EnumToStringMapper.cs would be necessary.
It seems that automapper only looks at the defined enum value but ignores [EnumMember(Value = "..")] attribute.
When converting the testDto to JSON it all works fine and displays the
EnumMember
's value.I'm not huge fan of this, but I'm working on an ASP.NET Core application which uses an legacy database and the values there are stored with dashes, so it'd be nice if AutoMapper respect EnumMember too. Using strings here isn't an option.
The text was updated successfully, but these errors were encountered: