仅包括返回一行而不关闭json

时间:2018-07-03 15:47:15

标签: sql-server rest orm .net-core entity-framework-core

我在使用.NET Core Api 2.1时遇到困难。

我在SQL Server 2017中建立了一个数据库,并使用所有正确的约定,FK,PK等创建了我的表。

表的结构如下:

联系人:

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; }
    }
}

地址:

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; }
    }
}

数字:

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; }
    }

电子邮件

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; }
}

除一个实体具有一对多关系外,所有实体都具有多对一关系。

然后我使用Entity Framework Core 2.1搭建了我的项目。

最初进行测试时,我没有.Include()我的地址,电话号码和电子邮件。联系人返回正常,直到我尝试.Include()其他属性,并且我的数据在第一数据行被截断而不关闭JSON。无论我做什么.Include(),都会发生这种情况。

我还会在Chrome中返回以下错误消息

Failed to load resource: net::ERR_SPDY_PROTOCOL_ERROR

这是我的原始项目,除了id不会返回关系表之外,该项目有效:

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

它返回此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":[]}]

这是我的包含声明:

[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 };
}

此呼叫返回:

[{"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

任何人都知道为什么数据会被截断吗?我没有意识到极限吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是一个link,我在这里找到了这个块。注意:关于发生的情况,有非常完整的描述。

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

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonNetFormatter(jsonSerializerSettings));