ASPNET Core - Json 响应在相关模型处停止

ASPNET Core - Json response stops at the related model

我有两个 class它们之间有关系。它们是市场和促销 classes。我在提出请求时遇到了问题。 json 结果在涉及关系时停止。

市场class:

    public class Market : BaseModel
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public string Address { get; set; }

        // GPS informations.
        public double Latitude { get; set; }
        public double Longitude { get; set; }

        [Required]
        public Guid PictureId { get; set; }
        public Picture Picture { get; set; }

        public List<Promotion> Promotions { get; set; }
    }

促销 class:

        public class Promotion : BaseModel
    {
        [Required]
        public string Description { get; set; }
        [Required]
        public double Price { get; set; }

        [Required]
        public Guid PictureId { get; set; }
        public Picture Picture { get; set; }

        [Required]
        public Guid MarketId { get; set; }
        public Market Market { get; set; }
    }

当我提出下一个请求时,我得到了一个不完整的答案。

        [HttpGet]
        [AllowAnonymous]
        public async Task<ActionResult<IEnumerable<Market>>> Get()
        {
            var markets = await _context.Markets
                .Include(m => m.Owner)
                .Include(m => m.Picture)
                .Include(m => m.Promotions)
                .ToListAsync();

            return markets;
        }

响应json 到达第一个促销的 MarketId 时停止。

    ...
        "pictureType": 0,
        "pictureUrl": "https://superbarato.azurewebsites.net/api/Pictures/url/d6bc07a8-db55-4ee5-7342-08d73f6147e9",
        "id": "d6bc07a8-db55-4ee5-7342-08d73f6147e9",
        "createdAt": "2019-09-22T13:34:26.9367403",
        "updatedAt": "0001-01-01T00:00:00",
        "deletedAt": "0001-01-01T00:00:00",
        "ownerId": "75c1f286-c07f-4e50-dda0-08d73f61058f",
        "owner": null
    },
    "promotions": [
        {
            "description": "Açúcar Camil 1Kg",
            "price": 5.0,
            "pictureId": "e7af68b9-c053-4f4b-7344-08d73f6147e9",
            "picture": null,
            "marketId": "e2962be8-1a19-418a-6ce7-08d73f62308d"

如何获得所有优惠?

你的问题是 MarketPromotion types/instances 相互引用,你陷入了无限期地序列化这两个的循环。您可以通过投影与响应无关的数据库模型来解决它 model/structure 以避免这种情况。

[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<IEnumerable<MarketModel>>> Get()
{
    var markets = await _context.Markets
        .Select(m => new MarketModel {
            Name = m.Name,
            // Other properties needed to be serialized to response body
            Promotions = m.Promotions.Select(p => new PromotionModel {
                Description = p.Description,
                // Other properties needed to be serialized to response body
                MarketId = p.Market.Id
            }
        }
        .ToListAsync();

    return markets;
}

public class MarketModel
{
    public string Name { get; set; }

    // Other properties needed to be serialized to response body

    public List<PromotionModel> Promotions { get; set; }
}

public class PromotionModel
{
    public string Description { get; set; }

    // Other properties needed to be serialized to response body

    public Guid MarketId { get; set; }
}

希望对您有所帮助。

在 EF Core 中,您可以配置 Json.NET 以忽略它在对象图中找到的循环。这是在 Startup.cs.

中的 ConfigureServices(...) 方法中完成的
services.AddMvc()
    .AddJsonOptions(
        options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    );

另一种方法是使用 [JsonIgnore] 属性修饰其中一个导航属性,该属性指示 Json.NET 在序列化时不要遍历该导航 属性。

参考:https://docs.microsoft.com/en-us/ef/core/querying/related-data#related-data-and-serialization