如何在ASP.NET MVC中通过automapper实现映射

时间:2015-12-03 12:06:33

标签: asp.net-mvc entity-framework entity automapper

我有两节课。 PRODUCT 来自实体框架,产品来自域解决方案:以下是详细信息: PRODUCT类(来源):

namespace SportsStore.Domain
{
using System;
using System.Collections.Generic;

public partial class PRODUCT
{
    public int PRODUCT_ID { get; set; }
    public string PRODUCT_NAME { get; set; }
    public string PRODUCT_DESCRIPTION { get; set; }
    public decimal PRODUCT_PRICE { get; set; }
    public string PRODUCT_CATEGORY { get; set; }
}
}

产品类别(目的地):

namespace SportsStore.Domain.Entities
{
public class Product
{
    [HiddenInput(DisplayValue = false)]
    public int ProductId {get;set;}
    public string ProductName {get;set;}

    [DataType(DataType.MultilineText)]
    public string ProductDescription {get;set;}
    public decimal ProductPrice {get;set;}
    public string ProductCategory { get; set; }
}

}

以下是整个项目中使用的ProductViewModel:

   public class ProductsListViewModel
{

    public IEnumerable<Product> Products { get; set; }

    public PagingInfo _pagingInfo { get; set; }

    public PagingInfo PagingInfo { get; set; }

    public string CurrentCategory { get; set; }

}

这是我在项目中的映射,我有一个自动播放器来映射这两个。

public static class AutoMapperConfig
{
    public static void RegisterMappings()
    {

        AutoMapper.Mapper.CreateMap<PRODUCT, Product>();


        AutoMapper.Mapper.CreateMap<PRODUCT, Product>()
            .ForMember(dest => dest.ProductCategory,
                       opts => opts.MapFrom(src => src.PRODUCT_CATEGORY));

        AutoMapper.Mapper.CreateMap<PRODUCT, Product>()
            .ForMember(dest => dest.ProductDescription,
                       opts => opts.MapFrom(src => src.PRODUCT_DESCRIPTION));

        AutoMapper.Mapper.CreateMap<PRODUCT, Product>()
            .ForMember(dest => dest.ProductId,
                        opts => opts.MapFrom(src => src.PRODUCT_ID));


        AutoMapper.Mapper.CreateMap<PRODUCT, Product>()
            .ForMember(dest => dest.ProductName,
                        opts => opts.MapFrom(src => src.PRODUCT_NAME));

        AutoMapper.Mapper.CreateMap<PRODUCT, Product>()
            .ForMember(dest => dest.ProductPrice,
                        opts => opts.MapFrom(src => src.PRODUCT_PRICE));


    }
}

我在Global.asax.cs的 Application_Start 中调用RegisterMappings。

   protected void Application_Start()
    {
        AutoMapperConfig.RegisterMappings();
    }

在这种情况下,我从Entity框架中读取内容,它们的类型是PRODUCT,当我尝试将其分配给Product时,我收到了错误。我得到错误的地方在这里:

    public ActionResult List(string category,int page = 1)
    {
        List<PRODUCT> pr = null;

        using(ProductContext pb = new ProductContext())
        {

            ProductsListViewModel model = new ProductsListViewModel
            {
                //Error is here
                Products = pb.ProductsList.Where(p => category == null || p.PRODUCT_CATEGORY == category).OrderBy(i => i.PRODUCT_ID).Skip((page - 1) * PageSize).ToList().Take(PageSize).ToList(),
                _pagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = category == null ? pb.ProductsList.Count() : pb.ProductsList.Where(e => e.PRODUCT_CATEGORY == category).Count()
                },
                CurrentCategory = category
            };

            return View(model);
        }
    }

错误是这样的:

  

无法隐式转换类型&#39; System.Collections.Generic.List&#39;到&#39; System.Collections.Generic.IEnumerable&#39;。存在显式转换(您是否错过了演员?)

我怎样才能克服这一点?提前致谢。

1 个答案:

答案 0 :(得分:0)

错误表示您需要知道的一切。在某个地方,你试图将一个对象列表放到相同对象的IEnumerable中。

在我看来,你应该改变这条线

Products = pb.ProductsList.Where(p => category == null || p.PRODUCT_CATEGORY == category)
.OrderBy(i => i.PRODUCT_ID)
.Skip((page - 1) * PageSize)
.ToList().Take(PageSize).ToList()

Products = pb.ProductsList.Where(p => category == null || p.PRODUCT_CATEGORY == category)
.OrderBy(i => i.PRODUCT_ID)
.Skip((page - 1) * PageSize)
.Take(PageSize).AsEnumerable()