Skip to content

Commit 2e3c1f3

Browse files
authored
Fixes Issue #152. EF.Property not mapped correctly. (#153)
1 parent 22a1a46 commit 2e3c1f3

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

src/AutoMapper.Extensions.ExpressionMapping/PrependParentNameVisitor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ public PrependParentNameVisitor(ParameterExpression currentParameter, string par
1818
public string ParentFullName { get; }
1919
public Expression NewParameter { get; }
2020

21+
protected override Expression VisitParameter(ParameterExpression node)
22+
{
23+
if (object.ReferenceEquals(CurrentParameter, node))
24+
{
25+
return string.IsNullOrEmpty(ParentFullName)
26+
? NewParameter
27+
: ExpressionHelpers.MemberAccesses(ParentFullName, NewParameter);
28+
}
29+
30+
return base.VisitParameter(node);
31+
}
32+
2133
protected override Expression VisitTypeBinary(TypeBinaryExpression node)
2234
{
2335
if (!(node.Expression is ParameterExpression))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using Xunit;
6+
7+
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
8+
{
9+
public class CanMapParameterBodyFromChildReferenceWithoutMemberExpression
10+
{
11+
[Fact]
12+
public void Can_map_parameter_body_from_child_reference_without_member_expression()
13+
{
14+
// Arrange
15+
var config = new MapperConfiguration(c =>
16+
{
17+
c.CreateMap<TestCategory, TestProductDTO>()
18+
.ForMember(p => p.Brand, c => c.MapFrom(p => EF.Property<int>(p, "BrandId"))); ;
19+
20+
c.CreateMap<TestProduct, TestProductDTO>()
21+
.IncludeMembers(p => p.Category);
22+
});
23+
24+
config.AssertConfigurationIsValid();
25+
var mapper = config.CreateMapper();
26+
27+
var products = new List<TestProduct>() {
28+
new TestProduct { }
29+
}.AsQueryable();
30+
31+
//Act
32+
Expression<Func<TestProductDTO, bool>> expr = x => x.Brand == 2;
33+
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);
34+
35+
//Assert
36+
Assert.Equal("x => (Convert(Property(x.Category, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
37+
}
38+
39+
public class TestCategory
40+
{
41+
// Has FK BrandId
42+
}
43+
public class TestProduct
44+
{
45+
public TestCategory? Category { get; set; }
46+
}
47+
48+
public class TestProductDTO
49+
{
50+
public int Brand { get; set; }
51+
}
52+
}
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using Xunit;
6+
7+
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
8+
{
9+
public class CanMapParameterBodyWithoutMemberExpression
10+
{
11+
[Fact]
12+
public void Can_map_parameter_body_without_member_expression()
13+
{
14+
// Arrange
15+
var config = new MapperConfiguration(c =>
16+
{
17+
c.CreateMap<TestProduct, TestProductDTO>()
18+
.ForMember(p => p.Brand, c => c.MapFrom(p => EF.Property<int>(p, "BrandId")));
19+
});
20+
21+
config.AssertConfigurationIsValid();
22+
var mapper = config.CreateMapper();
23+
24+
var products = new List<TestProduct>() {
25+
new TestProduct { }
26+
}.AsQueryable();
27+
28+
//Act
29+
Expression<Func<TestProductDTO, bool>> expr = x => x.Brand == 2;
30+
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);
31+
32+
//Assert
33+
Assert.Equal("x => (Convert(Property(x, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
34+
}
35+
36+
public class TestProduct
37+
{
38+
// Empty, has shadow key named BrandId
39+
}
40+
41+
public class TestProductDTO
42+
{
43+
public int Brand { get; set; }
44+
}
45+
}
46+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
4+
{
5+
internal class EF
6+
{
7+
internal static object Property<T>(object p, string v)
8+
{
9+
throw new NotImplementedException();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)