AutoMapper 6 - How can I create a mapping that ignore and map a list of object

asp.net-mvc-5 automapper-6 ef-fluent-api entity-framework-6

Question

I'm a little newbie on AutoMapper, I don't find almost nothing about v6.0 on Stackoverflow and Github. I need help on this problem

I have this two Entities:

public class DocFinanceiro
{
    public int AutoId { get; set; }  

    public virtual ICollection<QuitacaoDocFinan> QuitacoesDocFinan { get; set; } 
}

public class QuitacaoDocFinan
{
        public int AutoId { get; set; }

        public int DocFinanceiroId { get; set; }

        public virtual DocFinanceiro DocFinanceiro { get; set; }

        public decimal ValorTotal { get; set; }
    }
}

And his ViewModels:

public class DocFinanceiroViewModel
{
        public DocFinanceiroViewModel()
        {
            ValorPago = QuitacoesDocFinan.Where(x => x.Cancelada == false).Sum(x => x.ValorTotal);
        }

        public virtual ICollection<QuitacaoDocFinanViewModel> QuitacoesDocFinan { get; set; }

        public decimal ValorPago { get; set; }
}

public class QuitacaoDocFinanViewModel
{
        public int AutoId { get; set; }

        public int DocFinanceiroId { get; set; }

        public virtual DocFinanceiroViewModel DocFinanceiro { get; set; }

        public decimal ValorTotal { get; set; }
}

And mapping between DocFinanceiro and DocFinanceiroViewModel:

    public class DomainToViewModelMappingProfile : Profile
    {
        public DomainToViewModelMappingProfile()
        {
            CreateMap<DocFinanceiro, DocFinanceiroViewModel>().ForMember(x => x.ValorPago, y => y.Ignore())                                                                                                            
                                                                    .MaxDepth(3)
                                                                    .PreserveReferences();

            CreateMap<QuitacaoDocFinan, QuitacaoDocFinanViewModel>();
        }
    }

This mapping works when I set only one of these property

.ForMember(x => x.ValorPago, y => y.Ignore())

or

.MaxDepth(1).PreserveReferences();

, but when I try two cause an exception. I search on everywhere, but no success.

And controller where that I make the mapping:

var documentos = Mapper.Map<IEnumerable<DocFinanceiro>, IEnumerable<DocFinanceiroViewModel>>(*repository*);

Sorry if make some mistake, but I don't what to do...

1
0
5/2/2017 12:07:07 PM

Popular Answer

You are trying to access QuitacoesDocFinan collection in DocFinanceiroViewModel constructor before initializing. Your DocFinanceiroViewModel should be something like this:

public class DocFinanceiroViewModel
{
    public virtual ICollection<QuitacaoDocFinanViewModel> QuitacoesDocFinan { get; set; }

    public decimal ValorPago
    {
        get
        {
            return QuitacoesDocFinan.Where(x => x.Cancelada == false).Sum(x => x.ValorTotal);
        }
    }
}
0
5/8/2017 2:51:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow