AutoMapper / AutoMapper

A convention-based object-object mapper in .NET.
https://automapper.org
MIT License
9.95k stars 1.75k forks source link

ProjectTo & FirstOrDefault on Polymorph-Type isn't translateable #4393

Closed flobiber closed 9 months ago

flobiber commented 9 months ago

Source/destination types


// Entities

public class Job
{
    public Guid Id { get; set; }
    public JobType Type { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public uint xmin { get; set; }
}

public class ProtocolJob : Job
{
    public string? Source { get; set; }

    public ProtocolSeverity Severity { get; set; }

    public string Message { get; set; }

    public List<string> Tags { get; set; }
}

public class EmailJob : Job
{
    public string Receiver { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

// DTOs

public class JobDto
{
    public Guid Id { get; set; }
    public JobType Type { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public uint xmin { get; set; }
}

public class ProtocolJobDto : JobDto
{
    public string? Source { get; set; }

    public ProtocolSeverity Severity { get; set; }

    public string Message { get; set; }

    public List<string> Tags { get; set; }
}

public class EmailJobDto : JobDto
{
    public string Receiver { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

Mapping configuration


        CreateMap<Entity.Job, JobDto>()
            .IncludeAllDerived();
        CreateMap<EmailJob, EmailJobDto>();
        CreateMap<ProtocolJob, ProtocolJobDto>();

Version: x.y.z

AutoMapper 13.0.1 AutoMapper.Collection 9.0.0 AutoMapper.Collection.EntityFrameworkCore 9.0.0 Microsoft.EntityFrameworkCore 8.0.1

Expected behavior

If i query with ProjectTo a single Element with FirstOrDefault and a filter expresion it should return this.

Actual behavior

If i run this command without a filter expresion everything is working fine. If i run this command with a filter expresion on PK to receive a single element, i get "could not be translated"-Exception.

When i do the exact same on a non polymorh-type both statements are working perfectly.

Steps to reproduce


    // Works:
    return await _mapper.ProjectTo<JobDto>(_database.Jobs).FirstOrDefaultAsync(cancellationToken) 

    // Dont work
    return await _mapper.ProjectTo<JobDto>(_database.Jobs).FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken) 
lbargaoanu commented 9 months ago

That looks like an EF Core issue. Check the execution plan. Run that as a LINQ statement, without AM. You should get the same result.

flobiber commented 9 months ago

For all those who are interested: https://github.com/dotnet/efcore/issues/33069

github-actions[bot] commented 8 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.