首页 > 解决方案 > 使用 ThenInclude 的实体框架 - 从链接实体中排除某些列

问题描述

我有一个查询返回Configuration如下所示:

    public JsonResult Configurations(int id)
    {
        var myConfiguration = dbContext.MyEntity
                                                    .Where(e => e.Id == id)
                                                    .Include(e => e.Group)
                                                    .ThenInclude(g => g.Configuration)
                                                    .ThenInclude(c => c.ConfigurationChildren)
                                                    .ThenInclude(cc => cc.ConfigurationGrandchildren)
                                                    .FirstOrDefault();
                                                    .Group?
                                                    .Configuration;

       return Json(myConfiguration);
    }

Configuration有一个Client我不想包含在返回的 Json 中的属性,并且每个ConfigurationGrandchildren都有一个Client我不想包含的属性。我如何排除它们?

标签: c#jsonentity-framework-core

解决方案


尝试在 Client 属性上方添加 [JsonIgnore]。

配置.cs

public class Configuration
{
     [JsonIgnore]
     public string Client { get; set; }

}

推荐阅读