Skip to content

Commit c862b54

Browse files
committed
better error
1 parent 90a6580 commit c862b54

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/AutoMapper/Execution/ExpressionBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public static Expression MapExpression(this IGlobalConfiguration configuration,
6868
bool nullCheck;
6969
if (typeMap != null)
7070
{
71+
typeMap.CheckProjection();
7172
var allowNull = memberMap?.AllowNull;
7273
nullCheck = !typeMap.HasTypeConverter && (destination.NodeType != ExpressionType.Default ||
7374
(allowNull.HasValue && allowNull != profileMap.AllowNullDestinationValues));

src/UnitTests/Projection/ProjectCollectionListTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,26 @@ public override bool Equals(object obj)
8787
}
8888
}
8989
}
90+
public class MapProjection : AutoMapperSpecBase
91+
{
92+
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
93+
{
94+
cfg.CreateProjection<Address, AddressDto>();
95+
cfg.CreateMap<Customer, CustomerDto>();
96+
});
97+
[Fact]
98+
public void ShouldNotMap() => new Action(() => Map<CustomerDto>(new Customer())).ShouldThrow<AutoMapperConfigurationException>().Message.ShouldBe("CreateProjection works with ProjectTo, not with Map.");
99+
public class Customer
100+
{
101+
public IList<Address> Addresses { get; set; }
102+
}
103+
public record class Address(string Street);
104+
public class CustomerDto
105+
{
106+
public IList<AddressDto> Addresses { get; set; }
107+
}
108+
public class AddressDto
109+
{
110+
public string Street { get; set; }
111+
}
112+
}

0 commit comments

Comments
 (0)