I just started with EF Core and noticed that they have got all kind of XXXAsync methods now. I started using them and hit the rock with the use of context.SaveChnagesAsync() while doing simple basic insert operation. It went like this :
public async void SaveUserAsync(User user)
{
using (var context = GetContextTransient())
{
Model.User contextUser = new Model.User
{
EmailId = user.EmailId,
UserName = user.UserName,
JoinedDateTime = DateTime.Now
};
await context.User.AddAsync(contextUser).ConfigureAwait(false);
await context.SaveChangesAsync(true).ConfigureAwait(false);
}
}
The above implementation was not inserting any records to the DB but doing simple change as below, then it worked :
public async Task<int> SaveUserAsync(User user)
{
using (var context = GetContextTransient())
{
Model.User contextUser = new Model.User
{
EmailId = user.EmailId,
UserName = user.UserName,
JoinedDateTime = DateTime.Now
};
await context.User.AddAsync(contextUser).ConfigureAwait(false);
int result = await context.SaveChangesAsync(true).ConfigureAwait(false);
return result;
}
}
Now I know doing aysnc - await with void return type is not recommended but still shouldn't 1st implementation work because I am doing await on context.SaveChangesAsync() ? Is my understanding of async- await correct or am I missing something?