【发布时间】:2018-08-15 04:12:45
【问题描述】:
我有一个简单的电话:
public IActionResult GetApplications()
{
var result = context.Applications.Include(a=> a.AplicantCompany)
.Include(c=>c.CreditorCompany)
.ToList();
return Ok(result);
}
返回:
{"result":[{"context":null,"applicationId":1003,"aplicantCompany":{"id":1,"creditCompid":12344,"name":"Kibon","industry":"ice cream","address":"RaiboW street","city":"Rio de Janeiro","province":"Rio de Janeiro","country":"Brazil","postalCode":"9034-394","style":"modern","yearsStablished":100,"numEmployees":600,"salesAnnual":"12343434","taxId":323,"functionOfScore":12,"dateCreated":"2018-08-09T14:14:38.3325627"},"applicantCompanyID":1,"creditRequested":33333,"creditTerm":1,"processStatus":1,"recommendation":1,"approved":true,"creditApproved":1200000,"dateCreated":"2018-08-09T14:18:32.900369","dateProcessed":"2018-12-12T00:00:00","creditorCompanyId":1,"creditorCompany":{"creditorCompanyId":1,"name":"IBM","address":"boulevard of dreams","city":"Toronto","province":"ON","country":"Canada","postalCode":"M1t 2t5","revenue":12343,"riskPreference":23,"creditLimit":123,"portofolioScore":5.5,"userName":"Ronald","password":"1234","dateCreated":"2018-08-09T12:09:18.0977008","applications":[
由于某种原因,Entity 在获取其余数据之前就崩溃了。
当我尝试使用属性时:
[HttpGet("/api/applications")]
public IActionResult GetApplications()
{
var result = context.Applications.Include(a=> a.AplicantCompany.Name)
.Include(c=>c.CreditorCompany.Name)
.ToListAsync();
return Ok(result);
}
我没有收到任何东西。有人知道发生了什么吗?
注意:我现在收到此错误:
System.InvalidOperationException:“属性“名称”不是实体类型“ApplicantCompany”的导航属性。 'Include(string)' 方法只能与 '.' 一起使用。导航属性名称的分隔列表。'
但是属性存在...
【问题讨论】:
-
你遇到异常了吗?
-
如果 Name 只是一个字符串,你不包括它,你只包括 CreditorCompany
-
能否提供
Applications背后的模型? -
你不应该使用
.Include(a=> a.AplicantCompany.Name),Include用于a navigation property,这是不同模型之间的关系,而不是像public string Name { get; set; }这样的自属性。您似乎一直在尝试加载Applications的所有属性。如果你在return Ok(result);上设置一个断点,你会看到预期的Applications吗?请注意,您需要将IActionResult更改为async Task<IActionResult>并将context.Applications.Include更改为await context.。您从哪里得到损坏的结果?尝试邮递员发送请求。
标签: c# entity-framework asp.net-core