C# 使用包含的实体框架-从链接实体中排除某些列
我有一个查询返回一个C# 使用包含的实体框架-从链接实体中排除某些列,c#,json,entity-framework-core,C#,Json,Entity Framework Core,我有一个查询返回一个配置,如下所示: public JsonResult Configurations(int id) { var myConfiguration = dbContext.MyEntity .Where(e => e.Id == id)
配置
,如下所示:
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中包含该属性,Configuration孙辈
每个人都有一个Client
属性,我不想包含该属性。如何排除它们?尝试在客户端属性上方添加[JsonIgnore]
配置.cs
不能排除通过EF Core导航属性修复填充实体导航属性,因为它们是EF Core必须维护的模型不变量的一部分。但是,您可以使用相应序列化程序提供的控件从序列化中排除任何内容。或者不使用实体进行序列化,而是使用投影DTO。请参阅EF核心文档部分。谢谢@Ivan Stoev。你有没有一个我如何在我的案例中实现这一点的例子?在建模文件夹上不美观。然后包括(entity=>new EntitylyTo{/*映射此处所需的属性,如Property1=entity.Property*/})
public class Configuration
{
[JsonIgnore]
public string Client { get; set; }
}