Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixes Issue #152. EF.Property not mapped correctly.
  • Loading branch information
BlaiseD committed Nov 25, 2022
commit 3bb5736facf1575a9ff0f111fac496c509c119c0
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public PrependParentNameVisitor(ParameterExpression currentParameter, string par
public string ParentFullName { get; }
public Expression NewParameter { get; }

protected override Expression VisitParameter(ParameterExpression node)
{
if (object.ReferenceEquals(CurrentParameter, node))
{
return string.IsNullOrEmpty(ParentFullName)
? NewParameter
: ExpressionHelpers.MemberAccesses(ParentFullName, NewParameter);
}

return base.VisitParameter(node);
}

protected override Expression VisitTypeBinary(TypeBinaryExpression node)
{
if (!(node.Expression is ParameterExpression))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapParameterBodyFromChildReferenceWithoutMemberExpression
{
[Fact]
public void Can_map_parameter_body_from_child_reference_without_member_expression()
{
// Arrange
var config = new MapperConfiguration(c =>
{
c.CreateMap<TestCategory, TestProductDTO>()
.ForMember(p => p.Brand, c => c.MapFrom(p => EF.Property<int>(p, "BrandId"))); ;

c.CreateMap<TestProduct, TestProductDTO>()
.IncludeMembers(p => p.Category);
});

config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();

var products = new List<TestProduct>() {
new TestProduct { }
}.AsQueryable();

//Act
Expression<Func<TestProductDTO, bool>> expr = x => x.Brand == 2;
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);

//Assert
Assert.Equal("x => (Convert(Property(x.Category, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
}

public class TestCategory
{
// Has FK BrandId
}
public class TestProduct
{
public TestCategory? Category { get; set; }
}

public class TestProductDTO
{
public int Brand { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapParameterBodyWithoutMemberExpression
{
[Fact]
public void Can_map_parameter_body_without_member_expression()
{
// Arrange
var config = new MapperConfiguration(c =>
{
c.CreateMap<TestProduct, TestProductDTO>()
.ForMember(p => p.Brand, c => c.MapFrom(p => EF.Property<int>(p, "BrandId")));
});

config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();

var products = new List<TestProduct>() {
new TestProduct { }
}.AsQueryable();

//Act
Expression<Func<TestProductDTO, bool>> expr = x => x.Brand == 2;
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);

//Assert
Assert.Equal("x => (Convert(Property(x, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
}

public class TestProduct
{
// Empty, has shadow key named BrandId
}

public class TestProductDTO
{
public int Brand { get; set; }
}
}
}
12 changes: 12 additions & 0 deletions tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/EF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
internal class EF
{
internal static object Property<T>(object p, string v)
{
throw new NotImplementedException();
}
}
}