9

I have an ASP.NET MVC 4 Web API app using EntityFramework for ORM.

In the JSON I return, there are some cases where the same child node is present for multiple parent nodes. In these cases, the first occurrence of the child node is fully visible with all it's members. Any subsequent occurrence shows up as a $ref to the first occurrence. I'd like instead to see the full object everytime it shows up in the JSON returned.

For example, instead of seeing:

    [{
    "$id": "1",
    "userId": 1,
    "Badge": {
        "$id": "2",
        "badgeId": 1,
        "badgeName": "Gold"
        }
    }, {
    "$id": "3",
    "userId": 2,
    "Badge": {
        "$ref": "2"
        }
    }]

i'd like to see:

    [{
    "$id": "1",
    "userId": 1,
    "Badge": {
        "$id": "2",
        "badgeId": 1,
        "badgeName": "Gold"
        }
    }, {
    "$id": "3",
    "userId": 2,
    "Badge": {
        "$id": "4",
        "badgeId": 1,
        "badgeName": "Gold"
        }
    }]

Basically I want to get rid of any "$ref" in the JSON. Is there a way?

Thanks!

2 Answers 2

1

An easy way is to edit the generated entity classes code. For each of the entity classes, there will be a [DataContract(IsReference=true)] attribute assigned.

Something like the following:

[EdmEntityTypeAttribute(NamespaceName="YourNamespace", Name="YourEntity")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class YourEntity : EntityObject
{

Change it to IsReference=false. That should do the trick.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry it took me forever to respond. i didn't know there was an answer. the work-around i resorted to at the time was to explicitly set some of the members of the object to null in the webservice before returning it. I will give this solution a shot as well. thanks!
Is there a way to do this with the fluent API instead of attributes?
0

In my case, I'm using the Entity model, I simply set an entity key for a unique field in my .edmx diagram table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.