0

im facing a problem with probably selfreference Looping: Model:

public class ProtectedAccount
{
    public int Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime Created { get; private set; }
    public DateTime? Changed { get; set; }
    public bool Disabled { get; set; }

    public virtual ICollection<ProtectedAccountMember> Members { get; set; }
    public virtual ProtectedAccountType Type { get; set; }
}


public class ProtectedAccountMember
{

    public int Id { get; set; }
    [StringLength(300)]
    public string Name { get; set; }

    [Index]
    public virtual ProtectedAccount ProtectedAccount { get; set; }
}

Controller:

[ResponseType(typeof(ProtectedAccount))]
[Route("ProtectedAccounts/{id}/Members")]
[HttpGet]
public IHttpActionResult GetProtectedAccountMembers(int id)
{
    var protectedAccount = db.ProtectedAccounts.Find(id);

    if (protectedAccount == null)
    {
        return NotFound();
    }

    return Ok(protectedAccount.Members.ToList());
    }

the data wich i receive for an GET seems to Loop recursive through all navigations:

[
  {
    "ProtectedAccount": {
      "Members": [
        {
          "Id": 2,
          "Name": "XXX, XX",

        },
        {
          "Id": 3,
          "Name": "XX, XX",

        }
      ],
      "Type": null,
      "Id": 25,
      "ObjectGUID": "76bf65e7-af60-4fe8-b3e1-90afbfd65b65",
      "Name": "XXX",

    },
    "Id": 1,
    "Name": "test",
  },
  {
    "ProtectedAccount": {
      "Members": [
        {
          "Id": 1,
          "Name": "test",

        },
        {
          "Id": 3,
          "Name": "XX, XX",
          "SamAccountName": "XX",
          "Disabled": false
        }
      ],
      "Type": null,
      "Id": 25,
      "ObjectGUID": "76bf65e7-af60-4fe8-b3e1-90afbfd65b65",
      "Name": "XXXX",

    },
    "Id": 2,
    "Name": "XX, XX",

  },
  {
    "ProtectedAccount": {a
      "Members": [
        {
          "Id": 1,
          "Name": "test",
          "SamAccountName": "XXX",
          "Disabled": false
        },
        {
          "Id": 2,
          "Name": "XX, XX",
          "SamAccountName": "XX",
          "Disabled": false
        }
      ],
      "Type": null,
      "Id": 25,
      "ObjectGUID": "76bf65e7-af60-4fe8-b3e1-90afbfd65b65",
      "Name": "XXX",
    },
    "Id": 3,
    "Name": "XX, XX",

  }
]

There is only one "ProtectedAccount" in the database. DO i have to use DTO to overcome this issue? I tried some configuration via the json formatsettings but didnt get any better results.

thepill
  • 61
  • 1
  • 6
  • Looks like you are victim to Json serialisation walking the whole object graph. It's not clear what you want to happen. Do you want no ProtectedCount objects at all? If so look at http://stackoverflow.com/questions/10169648/how-to-exclude-property-from-json-serialization – Alan Macdonald Feb 01 '16 at 18:51
  • Yes, it is always better to use a DTO rather than sending your database object directly to the client. – jvanrhyn Feb 01 '16 at 18:55
  • @Pr1m-e I re-read and it was clear but SO won't let me edit. I'm basically suggesting adding the attribute on the ProtectedAccount reference inside the Member class but if you have many API calls to consider then DTO might be the way to go – Alan Macdonald Feb 01 '16 at 19:02
  • Or you could do a projection query to get the data in the shape that you want. – Preet Singh Feb 02 '16 at 16:07
  • DTO or JavaScriptSerializer.RecursionLimit property can be used. – Anil Feb 05 '16 at 15:19

1 Answers1

0

From your code, you are only returing the protectedAccount.Members, hence you could do a projection query as below

var results = ctx.ProtectedAccountMembers
                    .Where(member => member.ProtectedAccount.Id == protectedAccount.Id)
                    .Select(member => new { member.Id, member.Name }).ToList();
Preet Singh
  • 1,791
  • 13
  • 16