Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 列表<;对象>;序列化不能处理多个对象_Json_Entity Framework_Serialization_Asp.net Web Api_Json.net - Fatal编程技术网

Json 列表<;对象>;序列化不能处理多个对象

Json 列表<;对象>;序列化不能处理多个对象,json,entity-framework,serialization,asp.net-web-api,json.net,Json,Entity Framework,Serialization,Asp.net Web Api,Json.net,我有以下课程 [DataContract] public class Video { [Key] [DataMember(IsRequired = false)] public int VideoId { get; set; } [DataMember(IsRequired = false)] public int UserId { get; set; } [Required] [DataMember ] public stri

我有以下课程

[DataContract]
public class Video
{
    [Key]
    [DataMember(IsRequired = false)]
    public int VideoId { get; set; }

    [DataMember(IsRequired = false)]
    public int UserId { get; set; }

    [Required]
    [DataMember ]
    public string Title { get; set; }

    [Required]
    [DataMember]
    public virtual IList<Tag> Tags { get; set; }

}

[DataContract]
public class Tag
{
    [Key]
    [Required]
    [DataMember(IsRequired = false)]
    public int? TagId { get; set; }

    [DataMember(IsRequired = true)]
    [Required]
    public string Name { get; set; }

    [IgnoreDataMember]
    public virtual IList<Video> Videos { get; set; }

}
这就叫:

    public IList<Video> GetVideos()
    {
        using (var db = CreateContext())
        {
            return db.Videos.Include("Tags").ToList();
        }
    }

出于某种原因-
标记
有时序列化正确,有时则不正确。知道我做错了什么吗?

在对象图中有循环引用。无法正确地对它们进行JSON序列化,序列化程序会检测到这种情况并自动进行引用(
$ref
)。当您使用EF加载对象图时,内存中的这些对象之间存在循环引用,这些引用无法在JSON中正确表示


我建议您使用视图模型断开循环引用图,然后通过导线发送视图模型,而不是直接返回自动生成的EF模型。

您的对象图中有循环引用。无法正确地对它们进行JSON序列化,序列化程序会检测到这种情况并自动进行引用(
$ref
)。当您使用EF加载对象图时,内存中的这些对象之间存在循环引用,这些引用无法在JSON中正确表示


我建议您使用视图模型打破循环引用图,然后通过导线发送视图模型,而不是直接返回自动生成的EF模型。

谢谢Darin。你有什么建议?编辑:我会尽快接受:)谢谢达林。你有什么建议?编辑:只要它允许,我就接受:)
    public IList<Video> GetVideos()
    {
        using (var db = CreateContext())
        {
            return db.Videos.Include("Tags").ToList();
        }
    }
 [{
    "$id": "8",
    "tags": [
        {
            // CORRECT SERIALIZATION
            "$id": "9",  
            "tagId": 1,
            "name": "Example",
            "count": 5
        }
    ],
    "videoId": 18,
    "userId": 3,
    "title": "Test Video",
    "thumbnail": "http://i.imgur.com/gV3J2Uf.png",
    "source": "test source"
},
 {
    "$id": "19",
    "tags": [
        {
            // WTF?
            "$ref": "9"
        }
    ],
    "videoId": 28,
    "userId": 6,
    "title": "Test Video",
    "thumbnail": "http://i.imgur.com/gV3J2Uf.png",
    "source": "test source"
},
{
    "$id": "20",
    "tags": [
        {
            // CORRECT AGAIN
            "$id": "21",
            "tagId": 10,
            "name": "funny",
            "count": 2
        }
    ],
    "videoId": 29,
    "userId": 6,
    "title": "TEST VID",
    "thumbnail": "https://i.imgur.com/SWOQSOf.jpg",
    "source": "test source"
},
{
    "$id": "22",
    "tags": [
        {
            // INCORRECT
            "$ref": "9"
        },
        {
            "$ref": "21"
        }
    ],
    "videoId": 30,
    "userId": 6,
    "title": "TEST VID",
    "thumbnail": "https://i.imgur.com/R7lVobX.jpg",
    "source": "test source"
}