-1

I am using entity framework and asp.net web API. I want to make the following method async but I do not know where to use await in following code.

[HttpPost]
public async Task<IHttpActionResult> CreateAccount([FromUri]string fullname, string email, string cnic,string username,string password, string logrol )
{

    using (var entity = new Smock_DBEntities())
    {
        Person pr = new Person();
        pr.Full_Name = fullname;
        pr.Email = email;
        pr.CNIC = cnic;
        entity.Persons.Add(pr);
        entity.SaveChanges();
        int prID = entity.Persons.Where(per => per.CNIC == cnic).Select(per => per.Person_ID).First();

        Login log = new Login();
        log.Person_ID = prID;
        log.Username = username;
        log.Password = password;
        log.Login_Role = logrol;
        entity.Logins.Add(log);
        entity.SaveChanges();

        return Ok();
    }         
}
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Ans Bilal
  • 987
  • 1
  • 10
  • 28

1 Answers1

2

Putting it as an answer with possible places(3 places) where you can put await in your code snippet.

[HttpPost]
public async Task<IHttpActionResult> CreateAccount([FromUri]string fullname, string email, string cnic,string username,string password, string logrol )
{

    using (var entity = new Smock_DBEntities())
    {
        Person pr = new Person();
        pr.Full_Name = fullname;
        pr.Email = email;
        pr.CNIC = cnic;
        entity.Persons.Add(pr);
        await entity.SaveChangesAsync();
        int prID = await entity.Persons.Where(per => per.CNIC == cnic).Select(per => per.Person_ID).FirstAsync();

        Login log = new Login();
        log.Person_ID = prID;
        log.Username = username;
        log.Password = password;
        log.Login_Role = logrol;
        entity.Logins.Add(log);
        await entity.SaveChangesAsync();

        return Ok();
    }         
}