使用Entity Framework在我的Json响应中的$ id字段是什么?

时间:2017-08-02 09:49:37

标签: c# json entity-framework

为什么我的对象(不包含$ id,将它添加到我的Json响应中?)

Json回复:

enter image description here

型号:

public class DivisionWithProductsViewModel
{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Description {get;set;}
   public string Thumbnail {get;set;}
   public string ThumbnailName {get;set;}
   public List<ProductViewModel> ProductList {get;set;}
}

1 个答案:

答案 0 :(得分:3)

$id$ref字段用于在JSON中创建对象层次结构。

例如,请参阅此JSON:

{
   "people":[
      {
         "$id":1,
         "name":"John",
         "children":[
            { "$ref":2 }
         ]
      },
      {
         "$id":2,
         "name":"Jane"
      }
   ]
}

列表children中的对象与反序列化Jane时使用的对象完全相同。

如果使用JSON.NET对其进行反序列化,并且更改了Jane的名称,则会更新“孩子”。简也是因为它是同一个对象引用。