AutoMapper循环引用,我该如何映射它

时间:2016-10-24 10:29:46

标签: c# reference automapper circular-reference navigation-properties

我正在使用WPF,EF,AutoMapper ...... 我有一个问题,我如何将Dto映射到实体Dto有循环参考。 我试着像这样的Maped

public class AProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<DtoA, A>().MaxDepth(1);
        CreateMap<DtoB, B>().MaxDepth(1);
        CreateMap<DtoC, C>().MaxDepth(1);
    }
}    

但它不起作用。

var config = new MapperConfiguration(cfg => cfg.AddProfile<AProfile>());
var As = db.ADtos.ProjectTo<ADto>(config).ToList();

As [0]不为空,但As [0] .Bs为空,请帮助我!

namespace Opt
{
    public class ADto
    {
        public int Id {get;set;}
        public virtual List<BDto> Bs{get;set;}
        public virtual List<CDto> Cs{get;set;}
    }
    public class BDto
    {
        public int Id {get;set;}

        public int AId {get;set;}
        public virtual ADto A {get;set;}

        public virtual List<CDto> Cs{get;set;}
    }
    public class CDto
    {
        public int Id {get;set;}

        public int AId {get;set;}
        public virtual ADto A {get;set;}

        public int BId {get;set;}
        public virtual BDto B {get;set;}
    }
}
namespace Dest
{
    public class A
    {
        public int Id {get;set;}
        public virtual List<B> Bs{get;set;}
        public virtual List<C> Cs{get;set;}
    }
    public class B
    {
        public int Id {get;set;}

        public int AId {get;set;}
        public virtual A A {get;set;}

        public virtual List<C> Cs{get;set;}
    }
    public class C
    {
        public int Id {get;set;}

        public int AId {get;set;}
        public virtual A A {get;set;}

        public int BId {get;set;}
        public virtual B B {get;set;}
    }
}

0 个答案:

没有答案