'AutoMapper ProjectTo and EF Core Value Conversion for enum stored as string

How can we have AutoMapper's ProjectTo work with EF Core's Value Conversion for an enum stored as a string? The following code prints 0 instead of Blue

using Microsoft.EntityFrameworkCore;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using System;

await using var ctx = new MyDbContext(
    new DbContextOptionsBuilder()
    .UseSqlite("datasource=db.sqlite")
    .Options);
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();

ctx.House.Add(new House { Color = Color.Blue });
ctx.SaveChanges();

var config = new MapperConfiguration(_ => _.CreateMap<House, HouseDto>());
var house = ctx.House.ProjectTo<HouseDto>(config).Single();
Console.WriteLine(house.Color);

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions options) : base(options) { }
    public DbSet<House> House { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<House>().Property(_ => _.Color).HasConversion<string>();
    }
}
public class House
{
    public int Id { get; set; }
    public Color Color { get; set; }
}
public enum Color
{
    Blue = 1
}
public class HouseDto
{
    public ColorDto Color { get; set; }
}
public enum ColorDto
{
    Blue = 1
}

csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="10.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.13" />
  </ItemGroup>
</Project>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source