В Automapper отсутствует конфигурация карты типов или не поддерживается отображение? ASP> NET MVC - PullRequest
0 голосов
/ 09 января 2020

Ошибка при запуске этого метода .. в asp. net MVC

Конфигурация карты пропущенного типа Automapper или неподдерживаемое отображение

using (var onlineShopContext = new OnlineShopWebEntities())
{
    Order newOrder = AutoMapper.Mapper.Map<OrderView, Order>(order);
    var carts = onlineShopContext.Cart.Where(x => x.AspNetUserId == order.AspNetUserId);

    foreach (var cart in carts)
        newOrder.OrderDetails.Add(new OrderDetail { Product = cart.Product, Quantity = cart.Quantity, UnitPrice = cart.Product.Price });

    onlineShopContext.Order.Add(newOrder);
    onlineShopContext.Cart.RemoveRange(carts);
    onlineShopContext.SaveChanges();
}

Настройка класса Mapper

public static void ConfigureAutoMapper()
{
    Mapper.Initialize(cfg =>
    {
        // cfg.CreateMap<Asset, AssetView>();
        cfg.CreateMap<Product, ProductView>()
        .ForMember(vm => vm.ProductRating, m => m.MapFrom(u => u.ProductReviews != null && u.ProductReviews.Count > 0 ? (Convert.ToInt32(Math.Round(u.ProductReviews.Average(x => x.Rating), 0))) : 0));
        cfg.CreateMap<ProductView, Product>();
        cfg.CreateMap<Cart, CartView>();
        cfg.CreateMap<Category, CategoryView>();
        cfg.CreateMap<CategoryView, Category>();
        cfg.CreateMap<AspNetUsers, AspNetUsersView>();
        cfg.CreateMap<ProductReview, ProductReviewView>().ForMember(vm => vm.ReviewOwner, m => m.MapFrom(u => u.AspNetUsers.UserName));
        cfg.CreateMap<ProductReviewView, ProductReview>();
        cfg.CreateMap<Cart, CartView>();
        cfg.CreateMap<Order, OrderView>();
        cfg.CreateMap<OrderView, Order>().ForMember(x => x.OrderDetails, opt => opt.Ignore());
        cfg.CreateMap<OrderDetail, OrderDetailView>();
        cfg.CreateMap<OrderStatus, OrderStatusView>();
    });
}

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        AreaRegistration.RegisterAllAreas();
        StructuremapMvc.Start();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AutoMapConfig.ConfigureAutoMapper();
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}
...