在.NET Core中转换无限嵌套对象

vai*_*dil 1 c# .net-core asp.net-core

编辑:我最初说这个问题非常糟糕,说明问题是JSON序列化.当我使用自定义映射从基类转换为返回的模型时,实际上会出现问题.我为这种困惑道歉.:(

我正在使用.NET Core 1.1.0,EF Core 1.1.0.我正在查询兴趣并希望从我的数据库中获取其类别.EF正在查询数据库,没有问题.问题是返回的类别有一个感兴趣的集合,它有一个父类别,它有一个感兴趣的集合等.当我尝试将它从基类转换为我的返回模型时,我得到一个堆栈溢出,因为它试图转换无限循环的对象.我可以解决这个问题的唯一方法是在序列化类别之前将该集合设置为null.

兴趣/类别就是一个例子,但这种情况发生在我查询的所有实体中.他们中的一些人使用循环来将相关属性设置为null(例如帖子/评论)非常混乱.

解决这个问题的最佳方法是什么?现在我正在使用我编写的自定义映射来转换基类和返回的模型,但我愿意使用任何其他可能有用的工具.(我知道我的自定义映射是堆栈溢出的原因,但在将基类从基类投射到模型之前,肯定必须有更优雅的方法来处理它,而不是将所有内容设置为null.)

类别:

public class InterestCategory
{
    public long Id { get; set; }
    public string Name { get; set; }
    public ICollection<Interest> Interests { get; set; }
}

public class Interest
{
    public long Id { get; set; }
    public string Name { get; set; }
    public long InterestCategoryId { get; set; }
    public InterestCategory InterestCategory { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

楷模:

public class InterestCategoryModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public List<InterestModel> Interests { get; set; }
}

public class InterestModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public InterestCategoryModel InterestCategory { get; set; }
    public long? InterestCategoryId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

映射功能:

public static InterestCategoryModel ToModel(this InterestCategory category)
{
    var m = new InterestCategoryModel
    {
        Name = category.Name,
        Description = category.Description
    };

    if (category.Interests != null)
        m.Interests = category.Interests.Select(i => i.ToModel()).ToList();

    return m;
}

public static InterestModel ToModel(this Interest interest)
{
    var m = new InterestModel
    {
        Name = interest.Name,
        Description = interest.Description
    };

    if (interest.InterestCategory != null)
        m.InterestCategory = interest.InterestCategory.ToModel();

    return m;
}
Run Code Online (Sandbox Code Playgroud)

这是由查询返回的.(对不起,需要审查一些事情.)

嵌套对象

Tse*_*eng 8

这不是.NET Core相关的!JSON.NET正在进行序列化.

要在全局禁用它,只需在配置期间添加它 Startup

services.AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    }));
Run Code Online (Sandbox Code Playgroud)

编辑:

是否可以从模型中删除循环引用并具有2对不同的模型,具体取决于您是否要显示类别或兴趣?

public class InterestCategoryModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public List<InterestModel> Interests { get; set; }

    public class InterestModel
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
}

public class InterestModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public InterestCategoryModel InterestCategory { get; set; }

    public class InterestCategoryModel
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,每个模型都有一个嵌套类用于它的子对象,但它们的后引用已被删除,因此在反序列化期间不会有无限引用?