Sql server 只包括返回一行,不包括关闭json

Sql server 只包括返回一行,不包括关闭json,sql-server,rest,orm,.net-core,entity-framework-core,Sql Server,Rest,Orm,.net Core,Entity Framework Core,我在使用.NET核心Api 2.1时遇到困难 我在SQL Server 2017中建立了一个数据库,并使用所有适当的约定(FK、PK等)创建了我的表 这些表格的结构如下: 联系人: namespace ContactsApi { public partial class Contacts { public Contacts() { Addresses = new List<Addresses>();

我在使用.NET核心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; }
    }
}
编号:

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; }
}
所有实体都具有多对一关系,只有一个实体具有一对多关系

然后,我使用EntityFrameworkCore2.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":[]}]
以下是我的include声明:

[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
有人知道为什么数据会被截断吗?有我不知道的限制吗

谢谢大家!

这是我发现这块金块的地方。注意:这里有一个非常完整的描述

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

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

请使用实际的类代码而不是伪代码进行更新。否则很难评估,可能是猜测。@JohnWhite用类更新了。我也可以提供完整的方法,但是可以找到所有代码。使用ObjectResult而不是JsonResult有什么特殊原因吗?也许JsonResult会起作用better@onemorecupofcoffee没有刻意的原因,这只是我被教导的方式。用JsonResult测试后,没有明显变化。哦,循环参考!请注意文件的截断位置。就在第一个地址的联系人之前。有一个设置告诉JSON格式化程序忽略循环引用。应该可以了。肯定帮了大忙!我现在正在获取我的数据,但它已经被严格化了。在我的项目中,似乎找不到对
GlobalConfiguration.Configuration.Formatters
的引用,这是占位符还是我遗漏了什么?顺便说一句,我想你上面的链接可能被破坏了,我很确定我们已经解决了这个问题。API返回值中包含字符串数据可以吗?谢谢你的帮助!更新以修复链接,请查看。有更多的信息可以帮助你。
var jsonSerializerSettings = new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
};

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