简体   繁体   中英

ASP NET Core crashing when using eager load with Entity Framework

I have this simple call:

 public IActionResult GetApplications()
 {
        var result = context.Applications.Include(a=> a.AplicantCompany)
            .Include(c=>c.CreditorCompany)
            .ToList();
        return Ok(result);
 }

Which returns:

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

For some reason, Entity broke before getting the rest of the data.

And when I try to use a property:

[HttpGet("/api/applications")]
public IActionResult GetApplications()
{
    var result = context.Applications.Include(a=> a.AplicantCompany.Name)
                .Include(c=>c.CreditorCompany.Name)
                .ToListAsync();
    return Ok(result);
}

I don't receive anything. Does anyone have any idea what's happening?

Note: I'm now receiving this error:

System.InvalidOperationException: 'The property 'Name' is not a navigation property of entity type 'ApplicantCompany'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.'

But the property exists...

Your call to ToListAsync() is returning a Task , which may not have completed executing by the time you return from the method. The best solution would be to make the method async, like so:

public async Task<IActionResult> GetApplications()
{
   var result = await context.Applications.Include(a=> a.AplicantCompany)
      .Include(c=>c.CreditorCompany)
      .ToListAsync();
   return Ok(result);
}

These changes will cause the method to wait for all the results before continuing to the return statement, while allowing other requests to run while this one is waiting for its data.

The second case probably won't work, as EF wants to load the entire entity with Include . If you want to load another entity that's referenced by AplicantCompany , you can use

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

Actually, according to this answer , you might be able to do (I haven't tried it):

var result = await context.Applications.Select(a => new {
    Application = a,
    CreditorCompany = a.CreditorCompany.Name,
    ApplicantCompany = a.AplicantCompany.Name}).ToListAsync();

First of,

The Include() method is used to load related data, not properties on related data. Which means, to load the name property of the related AplicantCompany and CreditorCompany on the entity Applications, your first query is correct. It will include the name properties of both related entities

var result = context.Applications.Include(a=> a.AplicantCompany)
                .Include(c=>c.CreditorCompany)
                .ToList();

Then secondly, if you're using the async method ToListAsync() you need to await the task

[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);
}

Your call to ToListAsync is async; but the invocation and the method itself are synchronous. Change to ToList() or (better) make everything else asynchronous as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM