Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to do async calls correctly?

$
0
0

What is the difference between these two calls to the database? The first one excludes the async and await keywords on the Task that is happening. But the calling code from the controller awaits it, so what is the difference and what is the correct way?:

1)

in controller:

var company = await _repository.GetAsync(Id);

in repository:

public Task<Company> GetAsync(int Id)
{
     return _context.Companies.FirstOrDefaultAsync(i => i.Id == Id);
}

2)

var company = await _repository.GetAsync(Id);
public async Task<Company> GetAsync(Id)
{
     return await _context.Companies.FirstOrDefaultAsync(i => i.Id == Id);
}

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>