2014-10-17 97 views
0

我在我的方法中使用了AutoMapper,但不起作用。 请帮我改正它。自动映射器不起作用。

public virtual IEnumerable<TViewModel> FindAllByCriteria(Expression<Func<TModel, bool>> predicate = null, params Expression<Func<TModel, object>>[] includeProperties) 
    { 
     IQueryable<TModel> items = RepositoryContainer<TRepository>().FindAll(); 
     if (includeProperties != null) 
     { 
      foreach (var includeProperty in includeProperties) 
      { 
       items = items.Include(includeProperty); 
      } 
     } 
     var destination_VMList = new List<TViewModel>(); 
     var source_tModelList = new List<TModel>(); 
     source_tModelList = predicate != null ? items.Where(predicate).ToList() : items.ToList(); 

     Mapper.Map(source_tModelList, destination_VMList); // Error happened! 

     return destination_VMList; 
    } 

错误消息:

Missing type map configuration or unsupported mapping. 

Mapping types: 
Tag -> TagViewModel 
Jahan.Blog.Model.Tag -> Jahan.Blog.ViewModel.TagViewModel 

Destination path: 
List`1[0] 

Source value: 
System.Data.Entity.DynamicProxies.Tag_F1F5C39705DF9507B542CCC1A519D0757945F8E00B19F4F20C89F81DF8358563 
+2

您可以显示为此映射创建的AutoMapper配置文件吗? – 2014-10-17 20:12:30

+0

我是使用Auto Mapper的新手。你能帮我了解AutoMapper配置文件吗?我没有为它写任何AutoMapper配置文件。 – Jahan 2014-10-17 21:16:30

+2

我会说[this](http://consultingblogs.emc.com/owainwragg/archive/2010/12/15/automapper-profiles.aspx)是一个很好的资源。基本上,你创建一个扩展Profile的类,在那里你创建你的映射,声明自定义类型,并添加你的时髦的逻辑,如果属性不适合1:1。 – 2014-10-17 22:27:26

回答

0

您必须先配置地图:

Mapper.CreateMap<Tag , TagViewModel>(); 

然后,你可以映射这样的:

var destination_VMList = Mapper.Map<IEnumerable<TModel>, IEnumerable<TViewModel>>(source_tModelList);