AutoMapper Entities ViewModel

4

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.

    
asked by anonymous 01.12.2016 / 22:42

1 answer

0

Looking quickly at the images you posted, I see the following problem:

Your Model f% of the view is IEnumerable<UsuarioAtivosViewModel> . Obviously, a IEnumerable<T> does not have a property called LocalAtendimentoUsuario .

Assuming you correct this (e.g. @Model UsuarioAtivosViewModel ), your code indicates that this view model also does not have a property called LocalAtendimentoUsuario .

I think it's because of these inconsistencies that it's giving you the error.

(Of course, being this answer a year later, I think now has nothing to do.)

    
10.02.2017 / 23:38