Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Regression when calling ProjectTo to a basedto class on a DbSet of a subclass if a polymorphic mapping to a -different- subclass exists #4395

Closed
kev-andrews opened this issue Feb 14, 2024 · 0 comments · Fixed by #4402
Milestone

Comments

@kev-andrews
Copy link
Contributor

Source/destination types

abstract class Vehicle
{
	public int Id { get; set; }
	public string Name { get; set; }
}

class Car : Vehicle
{
	public int AmountDoors { get; set; }
}

class Motorcycle : Vehicle
{
	public bool HasSidecar { get; set; }
}

class VehicleDto
{
	public string Name { get; set; }
}

class MotorCycleDto : VehicleDto
{
	public bool HasSidecar { get; set; }
}

Mapping configuration

	cfg.CreateMap<Vehicle, VehicleDto>()
		.IncludeAllDerived();
	
	cfg.CreateMap<Motorcycle, MotorCycleDto>();

Version: 13.0.1

Expected behavior

 mapper.ProjectTo<VehicleDto>(context.Cars).ToList();

with context.Cars being a DbSet of Cars should not throw an error.

Actual behavior

an error is thrown:

No coercion operator is defined between types 'Car' and 'Motorcycle'

Steps to reproduce

Gist

This code works in Automapper 12. The issue seems to be caused by there being an explicit mapping for one of the Vehicle subclasses, but not the other. Adding an explicit map fixes the issue.

	cfg.CreateMap<Vehicle, VehicleDto>()
		.IncludeAllDerived();
	
	cfg.CreateMap<Motorcycle, MotorCycleDto>();
	cfg.CreateMap<Car, VehicleDto>();

Removing the map from Motorcycle to MotorcycleDto also fixes the issue, allowing

	mapper.ProjectTo<VehicleDto>(context.Cars).ToList();

to execute properly.

@AutoMapper AutoMapper locked and limited conversation to collaborators Feb 14, 2024
@lbargaoanu lbargaoanu converted this issue into discussion #4396 Feb 14, 2024
@lbargaoanu lbargaoanu added this to the 13.0.2 milestone Feb 20, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
2 participants