Automapper有时只能工作

时间:2016-02-07 15:10:44

标签: c# entity-framework asp.net-web-api automapper

我创建了一个web api,它返回一个DTO列表,我使用 automapper 来映射实体框架db第一种方法创建的实体列表与我的DTO实体。 有时它有时不工作(奇怪的行为),大多数时候它返回null。请看一下示例代码。

  public IEnumerable<RemarkHeaderViewModel> Get()
        {
            Mapper.CreateMap<RemarkHeader, RemarkHeaderViewModel>();
           //dbremarkdata have list of RemarkHeader
            var dbremarkdata = db.RemarkHeaders.ToList();

            //works sometimes, most of time return null
             var remarkHeaderVm = Mapper.Map<List<RemarkHeader>, List<RemarkHeaderViewModel>>(dbremarkdata);

            return remarkHeaderList;
        }

RemarkViewModel.cs

public partial class RemarkViewModel
    {

        public int RemarkId { get; set; }
        public int RemarkHeaderId { get; set; }
        public string RemarkBody { get; set; }
        public virtual RemarkHeaderViewModel RemarkHeaderViewModel { get; set; }
    }

Remark.cs 由实体框架生成

public partial class Remark
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Remark()
        {
            this.Packages = new HashSet<Package>();
        }

        public int RemarkId { get; set; }
        public int RemarkHeaderId { get; set; }
        public string RemarkBody { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Package> Packages { get; set; }
        public virtual RemarkHeader RemarkHeader { get; set; }
    }

AutoMapper_Mapping.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using PlannerDataLib;
using PlannerDataLib.DataModel;
using PlannerDataLib.ViewModel;

namespace PlannerWebAPI
{
    public class AutoMapper_Mapping
    {
        public static void Register_AutoMapper()
        {
            Mapper.CreateMap<Customer, CustomerViewModel>();
            Mapper.CreateMap<Order, OrderViewModel>();
            Mapper.CreateMap<Package, PackageViewModel>();
            Mapper.CreateMap<Planner, PlannerViewModel>();
            Mapper.CreateMap<Platform, PlatformViewModel>();
            Mapper.CreateMap<EndCustomer, EndCustomerViewModel>();
            Mapper.CreateMap<OrderType, OrderTypeViewModel>();
            Mapper.CreateMap<Incoterm, IncotermViewModel>();
            Mapper.CreateMap<ShippingOrganization, ShippingOrganizationViewModel>();
            Mapper.CreateMap<ShippingAddress, ShippingAddressViewModel>();
            Mapper.CreateMap<Country, CountryViewModel>();
            Mapper.CreateMap<OperatingUnit, OperatingUnitViewModel>();
            Mapper.CreateMap<PurchaseOrder, PurchaseOrderViewModel>();
            Mapper.CreateMap<OrderSource, OrderSourceViewModel>();
            Mapper.CreateMap<SalesPerson, SalesPersonViewModel>();
            Mapper.CreateMap<ApprovalStatu, ApprovalStatusVieModel>();
            Mapper.CreateMap<OrderPriority, OrderPriorityViewModel>();
            Mapper.CreateMap<Item, ItemViewModel>();
            Mapper.CreateMap<Remark, RemarkViewModel>();
            Mapper.CreateMap<RemarkHeader, RemarkHeaderViewModel>();


            Mapper.CreateMap<List<RemarkHeader>, List<RemarkHeaderViewModel>>();  
            Mapper.CreateMap<List<Package>, List<PackageViewModel>>();


            Mapper.CreateMap<PurchaseOrder, PurchaseOrderViewModel>()
              .ForMember(po => po.OrderSourceViewModel, m => m.MapFrom(md => md.OrderSource));

            Mapper.CreateMap<Order, OrderViewModel>()
                .ForMember(ov => ov.PackagesViewModel, m => m.MapFrom(md => md.Packages))
                .ForMember(ov => ov.EndCustomerViewModel, m => m.MapFrom(md => md.EndCustomer))
                .ForMember(ov => ov.OrderTypeViewModel, m => m.MapFrom(md => md.OrderType))
                .ForMember(ov => ov.IncotermViewModel, m => m.MapFrom(md => md.Incoterm))
                .ForMember(ov => ov.ShippingOrganizationViewModel, m => m.MapFrom(md => md.ShippingOrganization))
                .ForMember(ov => ov.ShippingAddressViewModel, m => m.MapFrom(md => md.ShippingAddress))
                .ForMember(ov => ov.OperatingUnitViewModel, m => m.MapFrom(md => md.OperatingUnit))
                .ForMember(ov => ov.PurchaseOrderViewModel, m => m.MapFrom(md => md.PurchaseOrder))
                .ForMember(ov => ov.SalesPersonViewModel, m => m.MapFrom(md => md.SalesPerson))
                .ForMember(ov => ov.SODViewModel, m => m.MapFrom(md => md.SalesPerson1))
                .ForMember(ov => ov.ApprovalStatusVieModel, m => m.MapFrom(md => md.ApprovalStatu))
                .ForMember(ov => ov.OrderPriorityViewModel, m => m.MapFrom(md => md.OrderPriority));

            Mapper.CreateMap<Package, PackageViewModel>()
                .ForMember(pv => pv.PlannerViewModel, m => m.MapFrom(md => md.Planner))
                .ForMember(pv => pv.PlatformViewModel, m => m.MapFrom(md => md.Platform))
                .ForMember(pv => pv.RemarkViewModel, m => m.MapFrom(md => md.Remark))
                .ForMember(pv => pv.PackageItemsViewModel, m => m.MapFrom(md => md.PackageItems));

            Mapper.CreateMap<EndCustomer, EndCustomerViewModel>()
                .ForMember(ev => ev.CustomerViewModel, m => m.MapFrom(md => md.Customer));

            Mapper.CreateMap<PackageItem, PackageItemViewModel>()
                .ForMember(piv => piv.ItemViewModel, m => m.MapFrom(md => md.Item));

            Mapper.CreateMap<ShippingAddress, ShippingAddressViewModel>()
               .ForMember(sav => sav.CountryViewModel, m => m.MapFrom(md => md.Country));

            Mapper.CreateMap<Remark, RemarkViewModel>()
               .ForMember(rs => rs.RemarkHeaderViewModel, m => m.MapFrom(md => md.RemarkHeader));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您应该只创建一次映射,理想情况是在程序初始化时 - 否则您可能会在AutoMapper执行映射的过程中重新创建映射,这可能会导致奇怪的行为。对于Web API项目,您可能会在Startup类的Configuration方法中执行此操作。