为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
MM们 2021-11-28 17:53:20
我有一个简单的电话: 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)' 方法只能与 '.' 一起使用。导航属性名称的分隔列表。但是属性存在...
查看完整描述

3 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

您对 的调用ToListAsync()正在返回 a Task,在您从该方法返回时它可能尚未完成执行。最好的解决方案是使方法异步,如下所示:


public async Task<IActionResult> GetApplications()

{

   var result = await context.Applications.Include(a=> a.AplicantCompany)

      .Include(c=>c.CreditorCompany)

      .ToListAsync();

   return Ok(result);

}

这些更改将导致方法在继续执行return语句之前等待所有结果,同时允许其他请求在此等待其数据时运行。


第二种情况可能不起作用,因为 EF 想要使用Include. 如果要加载引用的另一个实体,AplicantCompany可以使用


.Include(a => a.AplicantCompany).ThenInclude(c => c.Entity)

实际上,根据这个答案,您可能可以做到(我还没有尝试过):


var result = await context.Applications.Select(a => new {

    Application = a,

    CreditorCompany = a.CreditorCompany.Name,

    ApplicantCompany = a.AplicantCompany.Name}).ToListAsync();


查看完整回答
反对 回复 2021-11-28
?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

首先,


该Include()方法用于加载相关数据,而不是相关数据的属性。这意味着,要在实体 Applications 上加载相关 AplicantCompany 和 CreditorCompany 的 name 属性,您的第一个查询是正确的。它将包括两个相关实体的名称属性


var result = context.Applications.Include(a=> a.AplicantCompany)

                .Include(c=>c.CreditorCompany)

                .ToList();

其次,如果您使用异步方法 ToListAsync() 您需要等待任务


[HttpGet("/api/applications")]

public async Task<IActionResult> GetApplications()

{

    var result = await context.Applications.Include(a=> a.AplicantCompany)

                .Include(c=>c.CreditorCompany)

                .ToListAsync();

    return Ok(result);

}


查看完整回答
反对 回复 2021-11-28
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

你的调用ToListAsync是异步的;但是调用和方法本身是同步的。更改为ToList()或(更好)使其他所有内容也异步


查看完整回答
反对 回复 2021-11-28
  • 3 回答
  • 0 关注
  • 203 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信