2014-10-17 62 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);