调用Map泛型方法时,Automapper会抛出Stackoverflow [英] Automapper throws Stackoverflow when calling Map generic method

查看:106
本文介绍了调用Map泛型方法时,Automapper会抛出Stackoverflow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EntityFramework Core 2.0.1。首先使用代码生成此数据库

I am using EntityFramework Core 2.0.1. With code first I generate this database

I have models and entities, I am getting correctly my entities from my database, but when I want to map the collection to a modelcollection equivalent, I get a stackoverflow exception with no details!!!

var applications = DocumentService.GetApplications();
var res = Mapper.Map<IEnumerable<ApplicationModel>>(applications); //here I get the exception


I am loading my entities on this way:


public IEnumerable<Application> GetApplications()
{
    var apps = DocumentAppRepository.GetAll(new []{ "AppTagInfos.AppTagInfoDocuments.Document" }).ToList();

    return apps;
}

这是我的应用程序实体

public class AppTagInfo
{
    public int Id{get;set;}
    public int ApplicationId { get; set; }
    public int TagInfoId { get; set; }
    public Application Application { get; set; }
    public TagInfo TagInfo { get; set; }
    public bool IsRequired { get; set; }
    public IEnumerable<AppTagInfoDocument> AppTagInfoDocuments { get; set; }
}

TagInfo here:

public class TagInfo
{
    public int Id{get;set;}
    public Guid TagId { get; set; }
    public string TagName { get; set; }
    public int DataType { get; set; }
    public IEnumerable<AppTagInfo> AppTagInfos { get; set; } = new List<AppTagInfo>(); 
}


Per every class entity exist a  modelclass with the same fields but with Model
 suffix, the ApplicationModel is like this





public class ApplicationModel
{
    public int Id{get;set;}
    public Guid ApplicationId { get; set; }
    public string ApplicationName { get; set; }
    public IEnumerable<AppTagInfoModel> AppTagInfos { get; set; } = new List<AppTagInfoModel>();
}

我在这里错过了循环引用吗?

Am I missing a circular reference here?

Bellow是我每个实体的配置:

Bellow are my configurations per entity:

ApplicationConfiguration

public class ApplicationConfiguration : IEntityTypeConfiguration<Application>
{
    public void Configure(EntityTypeBuilder<Application> builder)
    {
        builder.ToTable("Application");
        builder.HasQueryFilter(app => !app.IsDeleted);

        builder.HasKey(app => app.Id);
        builder.Property(app => app.Id).ValueGeneratedOnAdd();
    }
}

AppTagInfoConfiguration

public class AppTagInfoConfiguration : IEntityTypeConfiguration<AppTagInfo>
{
    public void Configure(EntityTypeBuilder<AppTagInfo> builder)
    {
        builder.ToTable("AppTagInfo");
        builder.HasQueryFilter(d => !d.IsDeleted);

        builder.HasKey(dt => dt.Id);

        builder.Property(dt => dt.Id).ValueGeneratedOnAdd();

        builder.HasOne(dt => dt.Application)
            .WithMany(d => d.AppTagInfos)
            .HasForeignKey(dt => dt.ApplicationId)
            .HasConstraintName("ForeignKey_AppTagInfo_Application")
            .OnDelete(DeleteBehavior.Restrict)
            .IsRequired();

        builder.HasOne(dt => dt.TagInfo)
            .WithMany(t => t.AppTagInfos)
            .HasForeignKey(dt => dt.TagInfoId)
            .HasConstraintName("ForeignKey_AppTagInfo_TagInfo")
            .OnDelete(DeleteBehavior.Restrict)
            .IsRequired();
    }
}

TagInfoConfiguration

public class TagInfoConfiguration : IEntityTypeConfiguration<TagInfo>
{
    public void Configure(EntityTypeBuilder<TagInfo> builder)
    {
        builder.ToTable("TagInfo");
        builder.HasQueryFilter(d => !d.IsDeleted);

        builder.HasKey(t => t.Id);
        builder.Property(t => t.Id).ValueGeneratedOnAdd();
    }
}


Now one thing I notice is if I remove on the property
Application and TagInfo, then the Automapper works, seems like by default it falls on a recursive reference, going to Application and then to AppTagInfo back and forth, same with
TagInfo. The Automapper version I am using is v6.2.2 but I remember that when using
v4.0.30319 this kind of map worked







推荐答案

您能否清理帖子中的文字!!!
Can you please clean up the text in your post!!!


这篇关于调用Map泛型方法时,Automapper会抛出Stackoverflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆