Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

AutoMapper Flattering dont work in .net Core 2.0 #3980


User avatar
0
dfielec created

Hello in Asp.net Zero Core & Angulare v4.5.1 .net Core 2.0 solution, AutoMapper Flattering dont work.

thanks in advance.


8 Answer(s)
  • User Avatar
    0
    dfielec created

    In reality, the repository gives bad results. here an example

    public class Parent: FullAuditedEntity
        {
            [Required]
            public virtual string Name { get; set; }
    
            public virtual ICollection<Child> Children{ get; set; }
        }
    
    public class Child: FullAuditedEntity
        {
            [ForeignKey("ParentId")]
            public virtual Parent Parent{ get; set; }
            [Required]
            public virtual int ParentId { get; set; }
    
            public virtual string Name { get; set; }
        }
    
    [AutoMapFrom(typeof(Parent))]
        public classChildDto : FullAuditedEntityDto
        {
            public string Name { get; set; }
    
            public int ParentId { get; set; }
    
            public string ParentName { get; set; }
        }
    
    public class ChildAppService : MyAppServiceBase, IChildAppService
        {
            private readonly IRepository<Child> _childRepository;
    
            public ChildAppService(IRepository<Child> childRepository)
            {
                _childRepository = childRepository;
            }
    
            public async Task<ListResultDto<ParentDto>> GetChildren()
            {
                var children= await _childRepository.GetAllListAsync();
    
                return new ListResultDto<ChildDto>(ObjectMapper.Map<List<ChildDto>>(children));
            }
        }
    

    in the children list, the children[i].Parent is always null

  • User Avatar
    0
    aaron created
    Support Team

    EntityFrameworkCore requires eager-loading:

    var children = await _childRepository.GetAllIncluding(c => c.Parent).ToListAsync();
    
  • User Avatar
    0
    dfielec created

    Thank you very much aaroon, it works.

    Now, how to add multiple attributes?

  • User Avatar
    0
    soonjoo created

    <cite>DFIELEC: </cite> Thank you very much aaroon, it works.

    Now, how to add multiple attributes?

    var children = await _childRepository
    .GetAllIncluding(c => c.FirstParent)
    .Include(c => c.SecondParent)
    .ToListAsync();
    
  • User Avatar
    0
    aaron created
    Support Team

    GetAllIncluding accepts params, so simply use one or more commas:

    var children = await _childRepository
        .GetAllIncluding(
            c => c.Parent,
            c => c.AnotherRelationship)
        .ToListAsync();
    
  • User Avatar
    0
    dfielec created

    Thank you very much

  • User Avatar
    0
    jseger created

    So does this mean that extending AsyncCrudAppService won't work as expected? I mean, it won't include nested collections?

  • User Avatar
    0
    aaron created
    Support Team

    As explained above, EntityFrameworkCore requires eager-loading.