【发布时间】:2015-12-13 17:14:22
【问题描述】:
域组装中有很多相关的实体。例如,People 具有导航属性 (Level1) 到 FamilyRelations、Houses 和 Persons。除此之外,Houses 有自己的 nav.prop (Level2) 到 Address 和 Address (Level3) 必须到 City, Street ... 等等。
当我将 LazyLoadingEnabled 设置为 true 时,我将获得包含所有相关实体的 JSON(在屏幕左侧)。
我怎样才能只获得一个嵌套级别(如在 scree 的右侧)或将其他级别设置为 NULL 值(因为我设置了 Newtonsoft.Json.NullValueHandling.Ignore)?
我可以在每个实体不使用.Include 的情况下实现它吗?
我的班级:
public class People : BaseEntity
{
public int PersonID { get; set; }
public int HouseID { get; set; }
public int PeopleNumber { get; set; }
public int? FamilyRelationID { get; set; }
//FK to House
public virtual House Houses { get; set; }
//FK to Person
public virtual Person Persons { get; set; }
//FK to FamilyRelations
public virtual FamilyRelations FamilyRelations { get; set; }
}
WebAPI 配置:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling
= Newtonsoft.Json.NullValueHandling.Ignore;
我没有任何解决方案,因为我没有足够的经验。
所以,我需要你的建议,关于它的建议。对不起我的英语,如果我必须添加更多信息,请告诉我。谢谢
更新
我试图在映射类中添加[JsonIgnore] 或忽略这些属性,但是当我请求get/House 时,我需要在没有nav.prop 的情况下从Address 获取字段,而当请求get/People 时,我不需要@ 987654342@。结果我不能忽视它。
【问题讨论】:
标签: c# json asp.net-mvc entity-framework asp.net-web-api