本文介绍了检测到不支持的.Net Core 3.0可能的对象周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个相互关联的实体

I have 2 entities that are related as one to many

public class Restaurant {
   public int RestaurantId {get;set;}
   public string Name {get;set;}
   public List<Reservation> Reservations {get;set;}
   ...
}
public class Reservation{
   public int ReservationId {get;set;}
   public int RestaurantId {get;set;}
   public Restaurant Restaurant {get;set;}
}

如果我尝试使用自己的api获取预订的餐厅

If I try to get restaurants with reservations using my api

   var restaurants =  await _dbContext.Restaurants
                .AsNoTracking()
                .AsQueryable()
                .Include(m => m.Reservations).ToListAsync();
    .....

由于对象包含彼此的引用,因此我收到错误消息.有相关的帖子建议创建单独的模型或添加 NewtonsoftJson配置

I receive error in response, because objects contain references to each other.There are related posts that recommend to create separate modelor add NewtonsoftJson configuration

问题是我不想创建单独的模型,第二个建议也无济于事.有没有没有循环关系的加载数据的方法吗?*

Problem is that I do not want to create separate model and 2nd suggestion didn't help.Is there any way to load data without cycled relationship ?*

*

推荐答案

我已经在一个新项目中尝试了您的代码,安装软件包 Microsoft.AspNetCore.Mvc.NewtonsoftJson 最早用于3.0

I have tried your code in a new project and the second way seems to work well after installing the package Microsoft.AspNetCore.Mvc.NewtonsoftJson firstly for 3.0

services.AddControllersWithViews()
    .AddNewtonsoftJson(options =>
    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);

尝试一个新项目并比较差异.

Try with a new project and compare the differences.

这篇关于检测到不支持的.Net Core 3.0可能的对象周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:11