У меня проблемы с моим .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
У кого-нибудь есть идея, почему данные обрезаются? Есть ли ограничения, о которых я не знаю?
Спасибо!