1

I have the following classes

[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; }

}

In my WebAPI controller, I call this:

            var videos = _service.GetVideos();
            return Request.CreateResponse(HttpStatusCode.OK, videos);

Which calls this:

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

Yet over the wire, this is what I get:

 [{
    "$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"
}

For some reason - tags is sometimes serializing correctly, and sometimes not. Any idea what I'm doing wrong?


How about this....

var costs = firstOccurences.SelectMany (p => 
           from x in Enumerable.Range(0, (p.RepeatYears ?? 0) > 0
                               ? ((endDate.Year - p.Date.Year)+1)/p.RepeatYears.Value
                               : 1)
        let date = p.Date.AddYears(x * p.RepeatYears??0)
        where startDate <= date && endDate >= date
        select new PropertyCost {
          QuestionText=p.QuestionText,
          AnswerText=p.AnswerText,
          UnitCost = p.UnitCost,
          Date = date,
          RepeatYears = p.RepeatYears
        }
    )

You may need to convert firstOccurences to an Enumerable first due to translations functions

eg

          IEnumerable<PropertyCost> firstOccurences = (from aq in answeredQuestions
          from c in costs
          where aq.ID == c.AnsweredQuestionID
          select new PropertyCost
          {
              QuestionText = aq.Question.Text,
              AnswerText = aq.Answer.Text,
              UnitCost = c.Amount,
              Date = aq.PropertySurvey.Survey.StartDate,
              RepeatYears = c.RepeatPeriod
          }).AsEnumerable();

It's a quick fix and the initial 0 from the Enumerable.Range could be replaced with a calculation.

4

1 に答える 1

1

オブジェクト グラフに循環参照があります。JSON を適切にシリアライズすることはできません。シリアライザーはこの状態を検出し、自動的に参照を作成します ( $ref)。EF を使用してオブジェクト グラフを読み込んでいる場合、JSON で正しく表現できないメモリ内のオブジェクト間に循環参照があります。

自動生成された EF モデルを直接返すのではなく、ビュー モデルを使用して循環参照グラフを壊し、ネットワーク経由でビュー モデルを送信することをお勧めします。

于 2013-08-23T16:00:44.710 に答える