简体   繁体   中英

EF Automapper Parents child is null

I'm trying to read data from our database using entity framework and as the project already uses Automapper to convert from entities to Dtos it would make sense use Automappers Queryable Extensions to make life a bit easier. I'm using Microsoft.EntityFrameworkCore version 3.1.9.0

The problem is the returned array of BundleMetaDataDt.child is always null. The query below returns plenty of data, but every BundleMetaDataDtos child value is null.

I have tried:

  • .Include(b => b.ChildBundle) before Where statement
  • .ForMember(dest => dest.ChildBundle, opt => opt.MapFrom(src => src.ChildBundleId))
  • .MaxDepth(2)

Classes: (There is more fields than shown below)

public partial class Bundle
{
  public Guid? ChildBundleId { get; set; }
  public Bundle ChildBundle { get; set; }
}

public class BundleMetaDataDto
{
  [DataMember(IsRequired = true, Order = 15)]
  public BundleMetaDataDto ChildBundle { get; set; }
}

Map:

cfg.CreateMap<Bundle, BundleMetaDataDto>()
  .ForMember(dest => dest.ChildBundle, opt => opt.MapFrom(src => src.ChildBundle))

Query:

var bundles = context.Bundles
  .Where(bundle => bundle.ChildBundle != null)
  .ProjectTo<BundleMetaDataDto>(EntityConverter.MapperConfiguration)
  .ToArray();

感谢@LucianBargaoanu,我通过添加以下内容使其工作:

cfg.Advanced.RecursiveQueriesMaxDepth = 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM