jeudi 5 février 2015

AutoMapper ProjectUsing not being called

I'm trying to use the ProjectUsing feature of AutoMapper to only select the columns I need through a LINQ expression but it seems the expression itself does not get called at run-time or via unit testing.


As a test I am just putting a fixed value into the AlternateId property but the assert below always fails. This also fails with single instances (not in a queryable list) and at run-time via Entity Framework 6.



class MapFrom
{
public int Id { get; set; }
}
class MapTo
{
public int AlternateId { get; set; }
}

[TestMethod]
public void Automapper_projectusing_test()
{
AutoMapper
.Mapper
.CreateMap<MapFrom, MapTo>()
.ProjectUsing(src => new MapTo { AlternateId = 88 });
var products = new List<MapFrom>();
products.Add(new MapFrom());

var mapped = products
.AsQueryable() // Just in case ProjectUsing only works with IQueryable.
.Project()
.To<MapTo>()
.ToList();

Assert.AreEqual(88, mapped.Single().AlternateId); // Fails, AlternateId equals 0.
}


Using AutoMapper v3.3.1, NCrunch confirms that the mapping expression code is never executed.


Why is AutoMapper not executing this expression, perhaps I'm missing a key step?


Aucun commentaire:

Enregistrer un commentaire