自动找到1:1 mvc未映射的成员

时间:2018-03-20 18:59:49

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

我正在尝试使用automapper在我的entites db class和我的视图模型之间进行映射。他们有相同的道具名称,但我得到的错误是说找不到映射的成员。根据我的理解,如果你有1:1的关系,你不必在配置文件中进行手动映射。我在这里错过了什么?

产品类

public class product
{
    public int id { get; set; }
    public string sku { get; set; }
    public string ISBN { get; set; }
    public string itemName { get; set; }
    public int numberCds { get; set; }
    public string description { get; set; }
    public string category { get; set; }
    public double price { get; set; }
    public double weight { get; set; }
    public int stock { get; set; }
    public int stockAlert { get; set; }
    public string salesTax { get; set; }
    public string imgURL { get; set; }
    public string videoURL { get; set; }
    public int views { get; set; }
    public string instantDownload { get; set; }
    public string downloadLink { get; set; }
    public int active { get; set; }
    public string addedBy { get; set; }
    public DateTime addedTime { get; set; }
    public string updatedBy { get; set; }
    public DateTime updatedTime { get; set; }
}

参加课程

public partial class newProduct
{
    public int id { get; set; }
    public string sku { get; set; }
    public string ISBN { get; set; }
    public string itemName { get; set; }
    public Nullable<int> numberCds { get; set; }
    public string description { get; set; }
    public string category { get; set; }
    public double price { get; set; }
    public Nullable<double> weight { get; set; }
    public Nullable<int> stock { get; set; }
    public Nullable<int> stockAlert { get; set; }
    public string salesTax { get; set; }
    public string imgURL { get; set; }
    public string videoURL { get; set; }
    public Nullable<int> views { get; set; }
    public string instantDownload { get; set; }
    public string downloadLink { get; set; }
    public int active { get; set; }
    public string addedBy { get; set; }
    public Nullable<System.DateTime> addedTime { get; set; }
    public string updatedBy { get; set; }
    public Nullable<System.DateTime> updatedTime { get; set; }
}

映射配置         public static void RegisterMaps()         {

        AutoMapper.Mapper.Initialize(config =>
        {

            config.CreateMap<product, newProduct>();
            config.CreateMap<newProduct, product>();

        });


    }

和控制器

     public ActionResult Index()
    {
        using (StoreEntities db = new StoreEntities())
        {

            var results = (from p in db.newProducts select p).Where(a => a.active == 1);



            var products = AutoMapper.Mapper.Map<product>(results);


            return View(products);
        }

0 个答案:

没有答案