ASP NET Core 在 Entity Framework 中使用急切加载时崩溃

我有一个简单的电话:


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

出于某种原因,实体在获取其余数据之前就中断了。


当我尝试使用属性时:


[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: '属性 'Name' 不是实体类型 'ApplicantCompany' 的导航属性。'Include(string)' 方法只能与 '.' 一起使用。导航属性名称的分隔列表。


但是属性存在...


MM们
浏览 204回答 3
3回答

狐的传说

您对 的调用ToListAsync()正在返回 a Task,在您从该方法返回时它可能尚未完成执行。最好的解决方案是使方法异步,如下所示:public async Task<IActionResult> GetApplications(){&nbsp; &nbsp;var result = await context.Applications.Include(a=> a.AplicantCompany)&nbsp; &nbsp; &nbsp; .Include(c=>c.CreditorCompany)&nbsp; &nbsp; &nbsp; .ToListAsync();&nbsp; &nbsp;return Ok(result);}这些更改将导致方法在继续执行return语句之前等待所有结果,同时允许其他请求在此等待其数据时运行。第二种情况可能不起作用,因为 EF 想要使用Include. 如果要加载引用的另一个实体,AplicantCompany可以使用.Include(a => a.AplicantCompany).ThenInclude(c => c.Entity)实际上,根据这个答案,您可能可以做到(我还没有尝试过):var result = await context.Applications.Select(a => new {&nbsp; &nbsp; Application = a,&nbsp; &nbsp; CreditorCompany = a.CreditorCompany.Name,&nbsp; &nbsp; ApplicantCompany = a.AplicantCompany.Name}).ToListAsync();

摇曳的蔷薇

首先,该Include()方法用于加载相关数据,而不是相关数据的属性。这意味着,要在实体 Applications 上加载相关 AplicantCompany 和 CreditorCompany 的 name 属性,您的第一个查询是正确的。它将包括两个相关实体的名称属性var result = context.Applications.Include(a=> a.AplicantCompany)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include(c=>c.CreditorCompany)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();其次,如果您使用异步方法 ToListAsync() 您需要等待任务[HttpGet("/api/applications")]public async Task<IActionResult> GetApplications(){&nbsp; &nbsp; var result = await context.Applications.Include(a=> a.AplicantCompany)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Include(c=>c.CreditorCompany)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToListAsync();&nbsp; &nbsp; return Ok(result);}

小唯快跑啊

你的调用ToListAsync是异步的;但是调用和方法本身是同步的。更改为ToList()或(更好)使其他所有内容也异步
打开App,查看更多内容
随时随地看视频慕课网APP