C
C#•2y ago
HappyTeaKid

POST SQL Identity keeps returning id = 0 [Answered]

As you can see it keeps returning 0 value. It actually creates an object with different id in the database. How can I get the proper value?
14 Replies
HappyTeaKid
HappyTeaKid•2y ago
HappyTeaKid
HappyTeaKid•2y ago
The database is up to date using add-migration, update-database.
Pobiega
Pobiega•2y ago
show the code for the repo.Create method
HappyTeaKid
HappyTeaKid•2y ago
Pobiega
Pobiega•2y ago
and save just calls savechanges, I guess
HappyTeaKid
HappyTeaKid•2y ago
Yes await _context.SaveChangesAsync();
Pobiega
Pobiega•2y ago
but the save method is not awaited
Pobiega
Pobiega•2y ago
Pobiega
Pobiega•2y ago
if you call async code in that, you're fetching the ID before the save goes through
HappyTeaKid
HappyTeaKid•2y ago
I will add await before the Save and see if it fixes Yessssss, it works. thank you!
Pobiega
Pobiega•2y ago
Always be careful about mixing sync/async 🙂
HappyTeaKid
HappyTeaKid•2y ago
any command here to post it as solved? or just delete the post?
Pobiega
Pobiega•2y ago
/close
Accord
Accord•2y ago
✅ This post has been marked as answered!