I spent days to know the problems of my work, but no luck.

  1. I created new MVC4 Web API project.
  2. Add EF5 with my database (Project>Add>ADO.NET Entity Data Model>Create from database which is in Azure SQL).
  3. Add two tables to edmx as below. And two *.tt files generate entities and model classes successfully.

enter image description here

I can see the breakpoint(result) gives query result normally. But json gives abnormal stream without error message. (ie, http://localhost:41813/api/sheet/157 returns "157" which cannot download. in general, "157.json" is downloaded)

enter image description here

I copied properties in results to my handmade POCO-style class and it works.

What is my problem? I cannot use generated model classes to send data through Json. I hardly find out problem because no error message and no debug step available after the result breakpoint.

share|improve this question
Sounds like a problem with the json serialization. Can you try xml serialization (just add "Accept: application/xml" in request headers from the client) and see if you can download the data? – Sando Oct 25 '12 at 3:38
@Sando // applicatin/xml cannot solve. thanks for your comment. Below answer gives me the answer. – Youngjae Nov 2 '12 at 6:33

1 Answer

up vote 3 down vote accepted

The reason the serialization fails are yours Navigation Properties - while the serializer is trying to walk the object graph they result in circular dependencies.

For your simple sample to work you have few ways around it.

  1. Remove Navigation Property Sheet from SheetDetail
  2. Wrap your objects in ViewModel classes with Navigation Property Sheet omitted
  3. Create a metadata class with JsonIgnoreAttribute and then attach it to your entity with partial class and MetadataTypeAttribute

Here you can find sample for third solution (sample makes some assumptions as I don't know your exact data types):

public class SheetDetailSerializationMetadata
{
    [JsonIgnore]
    public Sheet Sheet { get; set; }
}

[MetadataType(typeof(SheetDetailSerializationMetadata))]
public partial class SheetDetail
{ 
}
share|improve this answer
+1, looks like circular object reference to me too. Here is a good link: asp.net/web-api/overview/formats-and-model-binding/… – danludwig Oct 26 '12 at 16:36
Thanks. your third suggestion solves my problem :) – Youngjae Nov 1 '12 at 10:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.