Reputation: 1793
I have the following classes. The domain models are created by entity framework and i am using POCO.
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedDate{ get; set; }
public DateTime ModifiedDate{ get; set; }
public virtual ICollection<Order> Orders{ get; set; }
}
public class CustomerDTO
{
public int Id { get; set; }
public string Name{ get; set; }
public List<OrderDTO> Orders{ get; set; }
}
public class Order
{
public int Id { get; set; }
public string Name { get; set; }
public int ProductId { get; set; }
public DateTime CreatedDate{ get; set; }
public DateTime ModifiedDate{ get; set; }
}
public class OrderDTO
{
public int Id { get; set; }
public string Name{ get; set; }
}
I have tried the following mappings.
Mapper.CreateMap<Customer, CustomerDTO>();
Mapper.CreateMap<CustomerDTO, Customer>();
Mapper.CreateMap<Order, OrderDTO>();
Mapper.CreateMap<OrderDTO, Order>();
I have also tried
Mapper.CreateMap<CustomerDTO, Customer>().ForMember(c => c.Orders,
m => m.MapFrom
(
q => Mapper.Map<List<OrderDTO>, ICollection<Order>>(q.Orders)
)
);
In order to update a customer i retrieve it from the DB and update it with the customerDTO
Customer customer = _customerRepository.GetById(customerDTO.Id);
Mapper.Map<CustomerDTO, Customer>(customerDTO, customer);
The customer object is updating correctly and the created and modified date aren't changed. But each order in the list of orders isnt updating correctly. Its productId, created and modified dates are being set to the default value and not the values that where retrieved from the database.
Do i have to do something else because Orders is a virtual collection?
I am new to auto mapper any help appreciated.
EDIT
I added
Mapper.AssertConfigurationIsValid();
Received the following error at app start: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
I updated my mapping to:
Mapper.CreateMap<OrderDTO, Order>()
.ForMember(x => x.CreatedDate, y => y.Ignore())
.ForMember(x => x.ModifiedDate, y => y.Ignore())
.ForMember(x => x.ProductId, y => y.Ignore())
But the Order is still having the above properties overwritten by the default vale
Upvotes: 4
Views: 6841
Reputation: 5144
I'm not sure its the correct or best solution, but in your mapping can you try changing
q => Mapper.Map<List<OrderDTO>, ICollection<Order>>(q.Orders)
to
q => Mapper.Map<List<OrderDTO>, ICollection<Order>>(q.Orders, c.Orders)
This should update rather than override the Orders (similar to your customer map).
Note that it should also be sufficient to just use:
q => Mapper.Map(q.Orders, c.Orders)
Aside:
Also a minor thing, I find it easier to read the mappings when using similar conventions to those in the documentation. For example:
// Configure AutoMapper
Mapper.CreateMap<CalendarEvent, CalendarEventForm>()
.ForMember(dest => dest.EventDate, opt => opt.MapFrom(src => src.EventDate.Date))
.ForMember(dest => dest.EventHour, opt => opt.MapFrom(src => src.EventDate.Hour))
.ForMember(dest => dest.EventMinute, opt => opt.MapFrom(src => src.EventDate.Minute));
Upvotes: 2