I have a scenario where my context returns a query for two User and CardCreditoUsuario entities, according to the following scenario below. During automapper action in my ViewModel, it is not mapping the entity CardCreditoUsuario for its properties exists in UserActiveViewModel.
Domain - User
public class Usuario
{
public Usuario()
{
Id = Guid.NewGuid().ToString();
CartaoCreditoUsuario = new List<CartaoCreditoUsuario.CartaoCreditoUsuario>();
}
public string Id{ get; set; }
public string Email{ get; set; }
public virtual ICollection<CartaoCreditoUsuario.CartaoCreditoUsuario> CartaoCreditoUsuario { get; set; }
...
}
Domain - CardCreditoUser
public class CartaoCreditoUsuario
{
public CartaoCreditoUsuario()
{
CartaoCreditoId = Guid.NewGuid();
}
public string Id { get; set; }
public Guid CartaoCreditoId { get; set; }
public bool Aura { get; set; }
public bool Elo { get; set; }
public bool Hipercard { get; set; }
public bool MasterCard { get; set; }
public bool Visa { get; set; }
public bool AmericanExpress { get; set; }
public virtual Usuario.Usuario Usuario { get; set; }
}
ViewModel - CardCreditoUsuarioAtivosViewModel
public class CartaoCreditoUsuarioAtivosViewModel
{
public bool Aura { get; set; }
public bool Elo { get; set; }
public bool Hipercard { get; set; }
public bool MasterCard { get; set; }
public bool Visa { get; set; }
public bool AmericanExpress { get; set; }
}
ViewModel - UserActiveViewModel
public class UsuarioAtivosViewModel
{
public UsuarioAtivosViewModel()
{
CartaoCreditoUsuario = new List<CartaoCreditoUsuarioAtivosViewModel>();
}
public string Id { get; set; }
public string Email { get; set; }
public ICollection<CartaoCreditoUsuarioAtivosViewModel> CartaoCreditoUsuario { get; set; }
...
}
DomainToViewModelMappingProfile
public class DomainToViewModelMappingProfile : Profile
{
protected override void Configure()
{
CreateMap<Usuario, UsuarioViewModel>();
CreateMap<Endereco, EnderecoViewModel>();
CreateMap<Usuario, RegisterViewModel>();
CreateMap<Endereco, RegisterViewModel>();
CreateMap<Usuario, UsuarioAtivosViewModel>();
CreateMap<AtendimentoUsuario, RegisterViewModel>();
CreateMap<LocalAtendimentoUsuario, RegisterViewModel>();
CreateMap<CartaoCreditoUsuario, RegisterViewModel>();
CreateMap<CartaoCreditoUsuario, CartaoCreditoUsuarioAtivosViewModel>();
CreateMap<CartaoCreditoUsuario, UsuarioAtivosViewModel>();
...
}
public class ViewModelToDomainMappingProfile : Profile
{
protected override void Configure()
{
CreateMap<UsuarioViewModel, Usuario>();
CreateMap<EnderecoViewModel, Endereco>();
CreateMap<RegisterViewModel, Usuario>();
CreateMap<RegisterViewModel, Endereco>();
CreateMap<UsuarioAtivosViewModel, Usuario>();
CreateMap<UsuarioAtivosViewModel, CartaoCreditoUsuario>();
CreateMap<RegisterViewModel, AtendimentoUsuario>();
CreateMap<RegisterViewModel, CartaoCreditoUsuario>();
CreateMap<RegisterViewModel, LocalAtendimentoUsuario>();
CreateMap<CartaoCreditoUsuarioAtivosViewModel, CartaoCreditoUsuario>();
...
}
}
As the image below, I have the following return from my context
Accordingtotheimagesabove,myreturnisasexpected,however,themomentIaccessthecollectionpropertyoftheCreditCreditoUsuariocollection,anexceptionispresentedbecausemyobjectisnullbecauseitdoesnotmappedaccordingtotheotherpropertiesofmyviewmodel,asshownbelow.
I would like a colleague's help with this problem if possible.
I already thank you in advance.