Skip to content

Instantly share code, notes, and snippets.

@kleberksms
Created October 22, 2016 16:24
Show Gist options
  • Save kleberksms/53477d865e238c6476f7e7a1247cc60f to your computer and use it in GitHub Desktop.
Save kleberksms/53477d865e238c6476f7e7a1247cc60f to your computer and use it in GitHub Desktop.
//Models
public class Comarca
{
public Comarca(){}
public Comarca(string descricao, Guid ufId)
{
Id = Guid.NewGuid();
Descricao = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(descricao);
UfId = ufId;
}
public Guid Id { get; private set; }
public virtual Uf Uf { get; set; }
public string Descricao { get; private set; }
public Guid UfId { get; private set; }
}
public class Uf
{
public Uf(){}
public Uf(string descricao, string sigla)
{
Descricao = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(descricao);
Sigla = CultureInfo.CurrentCulture.TextInfo.ToUpper(sigla);
Id =Guid.NewGuid();
}
public Guid Id { get; private set; }
public string Descricao { get; private set; }
public string Sigla { get; private set; }
public virtual ICollection<Comarca> Comarcas { get; set; }
}
//DTOs
public class ListagemComarcaViewModel
{
public Guid Id { get; set; }
public string Descricao { get; set; }
}
public class ListagemUfViewModel
{
public Guid Id { get; set; }
public string Descricao { get; set; }
public string Sigla { get; set; }
public List<ListagemComarcaViewModel> Comarcas { get; set; }
}
//Mapeamento
public IEnumerable<ListagemUfViewModel> RetonaListaViewModelUfComarca(List<Uf> list)
{
try
{
Mapper.Initialize(
cfg => cfg.CreateMap<Comarca, ListagemComarcaViewModel>()
.ForMember(dest => dest.Descricao, t => t.MapFrom(s => s.Descricao))
.ForMember(dest => dest.Id, t => t.MapFrom(s => s.Id))
);
Mapper.Initialize(cfg => cfg.CreateMap<Uf, ListagemUfViewModel>()
.ForMember(dest => dest.Comarcas, t => t.MapFrom(s =>
Mapper.Map<List<Comarca>,List<ListagemComarcaViewModel>>(s.Comarcas.ToList())
))
.ForMember(dest => dest.Descricao, t => t.MapFrom(s => s.Descricao))
.ForMember(dest => dest.Sigla, t => t.MapFrom(s => s.Sigla))
.ForMember(dest => dest.Id, t => t.MapFrom(s => s.Id))
);
var result = Mapper.Map<List<Uf>, List<ListagemUfViewModel>>(list);
Mapper.AssertConfigurationIsValid();
return result;
}
catch (AutoMapperConfigurationException exception)
{
throw new Exception(exception.ToString());
}
catch (AutoMapperMappingException exception)
{
throw new Exception(exception.ToString());
}
}
@kleberksms
Copy link
Author

AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
List1 -> List1
System.Collections.Generic.List1[[BPTrack.Models.Localidade.Uf, BPTrack, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List1[[BPTrack.ViewModel.ListagemConfiguracaoViewModel.ListagemUfViewModel, BPTrack, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ---> AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
Uf -> ListagemUfViewModel
BPTrack.Models.Localidade.Uf -> BPTrack.ViewModel.ListagemConfiguracaoViewModel.ListagemUfViewModel

Type Map configuration:
Uf -> ListagemUfViewModel
BPTrack.Models.Localidade.Uf -> BPTrack.ViewModel.ListagemConfiguracaoViewModel.ListagemUfViewModel

Property:
Comarcas ---> AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
List1 -> List1
System.Collections.Generic.List1[[BPTrack.Models.Localidade.Comarca, BPTrack, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List1[[BPTrack.ViewModel.ListagemConfiguracaoViewModel.ListagemComarcaViewModel, BPTrack, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
Comarca -> ListagemComarcaViewModel
BPTrack.Models.Localidade.Comarca -> BPTrack.ViewModel.ListagemConfiguracaoViewModel.ListagemComarcaViewModel
em lambda_method(Closure , Comarca , ListagemComarcaViewModel , ResolutionContext )
em AutoMapper.ResolutionContext.Map[TSource,TDestination](TSource source, TDestination destination)
em lambda_method(Closure , List1 , List1 , ResolutionContext )
--- Fim do rastreamento de pilha de exceções internas ---
em lambda_method(Closure , List1 , List1 , ResolutionContext )
em AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource source)
em AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
em lambda_method(Closure , List1 , List1 , ResolutionContext )
--- Fim do rastreamento de pilha de exceções internas ---
em lambda_method(Closure , List1 , List1 , ResolutionContext )
--- Fim do rastreamento de pilha de exceções internas ---
em lambda_method(Closure , List1 , List1 , ResolutionContext )
em AutoMapper.Mapper.AutoMapper.IMapper.Map[TSource,TDestination](TSource source)
em AutoMapper.Mapper.Map[TSource,TDestination](TSource source)
em BPTrack.Application.ConfiguracaoLogistica.ConfiguracaoLogisticaApplication.RetonaListaViewModelUfComarca(List`1 list) na C:\Users\Kleber\Documents\Visual Studio 2015\Projects\BPTrack\BPTrack\Application\ConfiguracaoLogistica\ConfiguracaoLogisticaApplication.cs:linha 101

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment