This project has moved. For the latest updates, please go here.


Mapping EFcomplex Entities into WCF DataContracts using AutoMapper


I'm new to AutoMapper and i have the following manual mapping in CustomerService in Service Layer in my application, where i tried to find out the equivalent AutoMapper mapping code but i couldn't due to nested and complex objects and collections, so please help me to resolve this issue

here the code from CustomerService : Service.Customer is the DataContract and Data.Customer is the Entity

public Service.Customer GetCustomer(string customerID)
    Data.Customer customer
        = _northwindEntities
                c => c.CustomerID == customerID);
    return new Service.Customer
                   CustomerID = customer.CustomerID,
                   CompanyName = customer.CompanyName,
                   ContactName = customer.ContactName,
                   Address = customer.Address,
                   City = customer.City,
                   Country = customer.Country,
                   Region = customer.Region,
                   PostalCode = customer.PostalCode,
                   Phone = customer.Phone,
                    = GetOrders(customer.Orders)
Service.Order is the DataContract and Data.Order is the Entity ...

private static IEnumerable<Service.Order> GetOrders(
    IEnumerable<Data.Order> order)
    return order.Select(o => new Service.Order
            OrderID = o.OrderID,
            OrderDate = o.OrderDate,
            OrderDetails = GetOrderDetails(o),
            Freight = o.Freight,
            ShippedDate = o.ShippedDate
Service.OrderDetail is the DataContract and Data.Order is the Entity ...

private static IEnumerable<Service.OrderDetail> GetOrderDetails(
    Data.Order order)

    return order.Order_Details.Select(
        o => new Service.OrderDetail
                = new Service.Product
                        = o.Product.ProductID,
                        = o.Product.ProductName
            Quantity = o.Quantity,
            UnitPrice = o.UnitPrice
So OrderDetails are nested inside Oreders and Orders are nested inside Customer ...How to reflect this hierarchy using AutoMapper Thanks in advance
Closed May 8, 2014 at 2:19 PM by jbogard