Я пытаюсь использовать Automapper QueryableExtensions для проектирования объекта на основе EF в dto, который включает в себя настраиваемый преобразователь. Он не работает с NRE в AutoMapper.PropertyMap.ResolveExpression. Я проследил код, и он терпит неудачу, потому что CustomExpression в классе PropertyMap имеет значение NULL.Automapper Project.To Null reference exception
Я могу легко воспроизвести это с помощью простой в памяти коллекции, как указано ниже (хотя я понимаю, что Project.To предназначен для использования с поддерживаемым LINQ-провайдером).
вот пример кода ... я пропустил что-то очевидное?
public class Contract
{
public string ContractNumber { get; set; }
public DateTime SettlementDateTime { get; set; }
}
public class ContractDto
{
public Settlement Settlement { get; set; }
}
public class Settlement
{
public string DeliveryLocation { get; set; }
public DateTime SettlementDateTime { get; set; }
}
public class ContractProfile : Profile
{
protected override void Configure()
{
IMappingExpression map = Mapper.CreateMap();
map.ForAllMembers(opts => opts.Ignore());
map.ForMember(v => v.Settlement, opts => opts.ResolveUsing());
}
}
public class SettlementCustomResolver:ValueResolver
{
protected override Settlement ResolveCore(Contract source)
{
return new Settlement() { DeliveryLocation = string.Empty, SettlementDateTime = source.SettlementDateTime };
}
}
internal class Program
{
private static void Main(string[] args)
{
ConfigureAutoMapper();
var contracts = new[]
{
new Contract { ContractNumber = "123", SettlementDateTime = DateTime.Today.AddDays(-1) },
new Contract { ContractNumber = "124", SettlementDateTime = DateTime.Today.AddDays(-2) }
};
//Exception happens on the following line...
var contractDtos = contracts.AsQueryable().Project().To<ContractDto>();
Console.Read();
}
private static void ConfigureAutoMapper(){
Mapper.AddProfile<ContractProfile>();
Mapper.AssertConfigurationIsValid();
}
}
}