我正在参与一个涉及C#异步编程的项目。我使用Automapper来映射Model和ViewModel。对于异步数据,我创建了一个以下的映射方法:
public static async Task<IEnumerable<PersonView>> ModelToViewModelCollectionAsync(this Task<IEnumerable<Person>> persons)
{
return await Mapper.Map<Task<IEnumerable<Person>>, Task<IEnumerable<PersonView>>>(persons);
}
我称此映射方法为以下内容(在我的服务类中):
public async Task<IEnumerable<PersonView>> GetAllAsync()
{
return await _personRepository.GetAllAsync("DisplayAll").ModelToViewModelCollectionAsync();
}
最后,我在控制器中调用了我的服务类。
public async Task<ActionResult> Index()
{
return View(await PersonFacade.GetAllAsync());
}
但是当我运行我的项目时,它显示以下异常。
Missing type map configuration or unsupported mapping.
Mapping types:
Task`1 -> Task`1
System.Threading.Tasks.Task`1[[System.Collections.Generic.IEnumerable`1[[PF.Model.Person, PF.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] -> System.Threading.Tasks.Task`1[[System.Collections.Generic.IEnumerable`1[[PF.Services.ViewModel.PersonView, PF.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Destination path:
Task`1
Source value:
System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[PF.Model.Person]]
根据我的项目架构,不可能避免使用automapper。
注意:获取所有内容的存储库方法如下:
public virtual async Task<IEnumerable<T>> GetAllAsync(string storedProcedure)
{
return await _conn.QueryAsync<T>(storedProcedure);
}