简体   繁体   中英

How can I do an async fetch of all the data from an entity with EF6.1?

I am using WebAPI and I tried to scaffold a controller. This is the method it came up with for a fetch by ID:

    // GET: api/ExamStatus/5
    [ResponseType(typeof(ExamStatus))]
    public async Task<IHttpActionResult> GetExamStatus(int id)
    {
        ExamStatus examStatus = await db.ExamStatus.FindAsync(id);
        if (examStatus == null)
        {
            return NotFound();
        }

        return Ok(examStatus);
    }

What if I wanted to do an async select of all the data from the ExamStatus entity? Can someone explain how I can do this?

You could try the .ToListAsync extension method:

[ResponseType(typeof(List<ExamStatus>))]
public async Task<IHttpActionResult> GetExamStatuses()
{
    var result = await db.ExamStatus.ToListAsync();
    return Ok(result);
}

Obviously this will select ALL the rows in your database and serialize them into the response.

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