0

I', new started to use async await method, i want my saved id back after successfully saved data. How can i do it with in one method only? currently i'm using this but it can't work...

var success = await _context.SaveChangesAsync() > 0;
return success ?  User.UserID : Guid.Empty;

simply want to get inserted NewGuid for insert(map another entity with it) in another my OTP log Table.

Arti Ahir
  • 13
  • 5
  • You need to give a more thorough code example so people can help. – scgough Jan 09 '20 at 16:50
  • I want inserted Id after successfully insert done to map inserted record with another entity. – Arti Ahir Jan 09 '20 at 16:58
  • What is the point of `await _context.SaveChangesAsync() > 0;` = specifically the `> 0` part? – Mark C. Jan 09 '20 at 17:04
  • Does this answer your question? [How can I get Id of inserted entity in Entity framework?](https://stackoverflow.com/questions/5212751/how-can-i-get-id-of-inserted-entity-in-entity-framework) – Mark C. Jan 09 '20 at 17:06
  • If there was an error then it returns -1 so we can identify Save changes done successfully or not. As per MS SQL it returns always value... either -1 on fail or 0 on success – Arti Ahir Jan 09 '20 at 17:08
  • Thank you but No, that is for without using async methods... – Arti Ahir Jan 09 '20 at 17:10
  • I use save both data(row) entry on same save Changes method... It worked properly. :D – Arti Ahir Jan 09 '20 at 17:40
  • Your wrapper method for the block of code you have presented must be marked as async for you to be able to return an asynchronously assigned variable. It would be helpful if you can share the full method with us. – Ozan Yurtsever Jan 10 '20 at 11:44

0 Answers0