include only returning one row and not closing json

.net-core entity-framework-core orm rest sql-server

Question

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!

1
0
7/4/2018 7:10:22 PM

Accepted Answer

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));
0
7/6/2018 1:40:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow