具有自动映射的层次结构嵌套对象

时间:2018-02-07 11:49:39

标签: c# entity-framework nested copy automapper

我的代码有问题。
当我尝试获取列表的副本时,如下面的代码中所示,我得到了堆栈溢出异常。

我的课程:

public partial class tbl_Accounts
{
    public tbl_Accounts()
    {
        AccountChilds   = new HashSet<tbl_Accounts>();
        GLLinks         = new HashSet<tbl_SLToGL>();
    }

    public int                          ID                  { get; set; }
    public short                        FK_Year             { get; set; }
    public byte                         FK_Company          { get; set; }
    public int?                         FK_Parent           { get; set; }
    public byte                         LevelAccount        { get; set; }
    public int                          Code                { get; set; }
    [Required]
    [StringLength(200)]
    public string                       Title               { get; set; }
    [StringLength(200)]
    public string                       TitleLatin          { get; set; }
    public byte                         Kind                { get; set; }
    public bool                         IsActive            { get; set; }
    public bool                         HasDL               { get; set; }
    public bool                         HasLevelFour        { get; set; }
    [Column(TypeName = "date")]
    public DateTime                     CreatDate           { get; set; }
    public  ICollection<tbl_Accounts>   AccountChilds       { get; set; }
    public  ICollection<tbl_SLToGL>     GLLinks             { get; set; }
    public  tbl_Accounts                AcountsParrent      { get; set; }
}

我的配置映射代码是:

var config = new MapperConfiguration(cfg =>
        {
            cfg.AddCollectionMappers();
            cfg.AllowNullCollections = true;

            cfg.CreateMap<tbl_SLToGL, tbl_SLToGL>()
                .ForMember(dest => dest.ID,             src => src.UseValue(0))
                .ForMember(dest => dest.FK_Year,        src => src.UseValue(Coding.ToYear))
                .ForMember(dest => dest.FK_SL,          src => src.UseDestinationValue());

            cfg.CreateMap<tbl_Accounts, tbl_Accounts>()
                .ForMember(dest => dest.FK_Year,        src => src.UseValue(Coding.ToYear))
                .ForMember(dest => dest.ID,             src => src.UseValue(0))
                .ForMember(dest => dest.AcountsParrent, src => src.Ignore())
                .ForMember(dest => dest.GLLinks,        src => src.MapFrom(x => x.GLLinks));
        });


        var ListSL = config
                        .CreateMapper()
                        .Map<IList<tbl_Accounts>, IList<tbl_Accounts>>(List);

当我运行代码时,我得到Exception并退出应用程序。
请帮帮我。

0 个答案:

没有答案