0

I'm having difficulty with my .NET Core Api 2.1.

I set up a database in SQL Server 2017 and created my tables with all the proper conventions, FK's, PK's and so on.

The tables are structured as follows:

Contacts:

namespace ContactsApi
{
    public partial class Contacts
    {
        public Contacts()
        {
            Addresses = new List<Addresses>();
            Emails = new List<Emails>();
            Numbers = new List<Numbers>();
        }

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Company { get; set; }
        public string ProfileImage { get; set; }
        public DateTime? Birthday { get; set; }
        public string Notes { get; set; }

        public List<Addresses> Addresses { get; set; }
        public List<Emails> Emails { get; set; }
        public List<Numbers> Numbers { get; set; }
    }
}

Addresses:

namespace ContactsApi
{
    public partial class Addresses
    {
        public int Id { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string CityRegion { get; set; }
        public string StateProvince { get; set; }
        public string ZipPostalCode { get; set; }
        public string Country { get; set; }
        public string Category { get; set; }
        public int ContactId { get; set; }

        public Contacts Contact { get; set; }
    }
}

Numbers:

public partial class Numbers
    {
        public int Id { get; set; }
        public string Category { get; set; }
        public string PhoneNumber { get; set; }
        public int ContactId { get; set; }

        public Contacts Contact { get; set; }
    }

Emails

public partial class Emails
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string Category { get; set; }
    public int ContactId { get; set; }

    public Contacts Contact { get; set; }
}

All entities have a many to one relationship except for one, which has a one to many relationship.

I then scaffolded my project using Entity Framework Core 2.1.

When initially testing, I did not .Include() my Addresses, Numbers, and Emails. The contacts returned just fine until I tried to .Include() the other properties and my data was truncated at the first data row without closing the JSON. This happens no matter what I .Include().

I also get back the following error message in Chrome

Failed to load resource: net::ERR_SPDY_PROTOCOL_ERROR

Here's my original project, which works except id does not return the relational tables:

[HttpGet]
public IActionResult GetContact()
{
    var joinedContacts = _context.Contacts;
    return new ObjectResult(joinedContacts) { StatusCode = 200 };
}

It returns this JSON:

[{"id":1,"firstName":"Ryan","lastName":"Peterson","company":"RK Peterson Media","profileImage":"ryanpeterson.jpg","birthday":"2018-06-24T00:00:00","notes":"Technically sophisticated Full Stack Web Developer with solid history of innovative solutions for a wide range of clients and businesses. Demonstrated success in web and application development with proficiency in front end, back end, UI/UX, coding, software, and application design. Excel at SEO-based web design, Google Analytics, PCI compliance, project management, and full life cycle software development (SDLC). Skilled trainer and project leader; able to concurrently lead the creation and launch of various websites for a diverse clientele.","addresses":[],"emails":[],"numbers":[]},{"id":2,"firstName":"Walter","lastName":"White","company":"Grey Matter","profileImage":"noimage.jpg","birthday":"2018-06-24T00:00:00","notes":"Seems a lot more irritable since the diagnosis... who can blame him trying to feed a family on a teachers salary?","addresses":[],"emails":[],"numbers":[]},{"id":3,"firstName":"Alejandro","lastName":"Rose-Garcia","company":"Pan American Drums","profileImage":"noimage.jpg","birthday":"2018-06-24T00:00:00","notes":"This guy!","addresses":[],"emails":[],"numbers":[]},{"id":4,"firstName":"Justin","lastName":"Trudeau","company":"Canada","profileImage":"noimage.jpg","birthday":"2018-06-24T00:00:00","notes":"Can explain quantum physics! Not clear on his policy, but at least he''s super cool!","addresses":[],"emails":[],"numbers":[]}]

Here's my include statement:

[HttpGet]
public IActionResult GetContact()
{
    var joinedContacts = _context.Contacts
                .Include(a => a.Emails)
                .Include(a => a.Addresses)
                .Include(a => a.Numbers)
                .ToList();
    // add Count of results for validation
    if (joinedContacts.Count > 0)
        return new ObjectResult(joinedContacts) { StatusCode = 200 };
    else
        return new ObjectResult(null) { StatusCode = 404 };
}

This call returns:

[{"id":1,"firstName":"Ryan","lastName":"Peterson","company":"RK Peterson Media","profileImage":"ryanpeterson.jpg","birthday":"2018-06-24T00:00:00","notes":"Technically sophisticated Full Stack Web Developer with solid history of innovative solutions for a wide range of clients and businesses. Demonstrated success in web and application development with proficiency in front end, back end, UI/UX, coding, software, and application design. Excel at SEO-based web design, Google Analytics, PCI compliance, project management, and full life cycle software development (SDLC). Skilled trainer and project leader; able to concurrently lead the creation and launch of various websites for a diverse clientele.","addresses":[{"id":3,"addressLine1":"223 W Jackson Blvd","addressLine2":null,"addressLine3":null,"cityRegion":"Chicago","stateProvince":"IL","zipPostalCode":"60606-6908","country":"United States","category":"Home","contactId":1

Anyone have an idea why the data is getting truncated? Are there limits I'm unaware of?

Thank you!

  • Please update with the actual class code instead of pseudo-code. Difficult to evaluate otherwise, would be guessing. – John White Jul 03 '18 at 18:21
  • @JohnWhite Updated with classes. I can provide the full method as well, but all code can be found [here](https://github.com/ryanpetersondesigns/Contacts) – Ryan Peterson Designs Jul 03 '18 at 19:21
  • Is there a specific reason why you're using ObjectResult instead of JsonResult? Maybe JsonResult will work better – onemorecupofcoffee Jul 04 '18 at 16:34
  • @onemorecupofcoffee No deliberate reason, it was just the way I was taught. After testing with JsonResult, no apparent change. – Ryan Peterson Designs Jul 04 '18 at 19:08
  • Oh Circular reference! Notice **where** the file cut off. Right before the Contact in the first Address. There is a setting to tell JSON formatter to ignore circular references. That should do it. – John White Jul 05 '18 at 19:08

1 Answers1

0

Here's a link where I found this nugget. NOTE: there's a very complete description about what happens.

var jsonSerializerSettings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
};

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonNetFormatter(jsonSerializerSettings));
John White
  • 705
  • 4
  • 12
  • Definitely helped a lot! I'm getting my data now, but it's been stringified. I can't seem to find a reference to `GlobalConfiguration.Configuration.Formatters` anywhere in my project, is that a placeholder or am I missing something? By the way, I think your link above may be broken, [this is the one that worked for me](https://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm) I'm pretty sure we're just about done solving this problem. Is it okay to have stringy data in an API return value? Thanks for your help! – Ryan Peterson Designs Jul 06 '18 at 03:41
  • Updated to fix link, take a look. Much more information there to help you. – John White Jul 06 '18 at 13:43