.Net Core 3.0 possible object cycle was detected which is not supported

前端 未结 8 1687
我寻月下人不归
我寻月下人不归 2020-12-02 09:27

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          


        
8条回答
  •  温柔的废话
    2020-12-02 10:21

    public class Reservation{ 
    public int ReservationId {get;set;} 
    public int RestaurantId {get;set;} 
    [JsonIgnore]
    public Restaurant Restaurant {get;set;} 
    

    Above worked also. But I prefer the following

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

    Because first we need to add the attribute to all the models we may have the cyclic reference.

提交回复
热议问题