Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using Find() method with async/await

I have sync method i MVC controller , which I want to make it async
it had this in the controller

  var user = _context.Users.Find(UserId);
  cart.User = user;
  _context.SaveChanges();
 

but when I change to await ,

  var user =await _context.Users.Find(UserId);
  cart.User = user;
  _context.SaveChanges();

it gives me error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

User doesnot contain definition for ‘Getawaiter’ and no accessible extension accepting a first argument of type User…….

but if I change it to

 var user = await _context.Users.Where(p=>p.Id==UserId).FirstOrDefaultAsync();
 cart.User = user;
 _context.SaveChanges(); 

it works fine. Cann’t I use find method with async/await???

>Solution :

You can’t just make any method async by putting await before it. There has to be an async version of the method.

EFCore: DbSet.FindAsync()

EF: DbSet.FindAsync()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading