I have an EF helper class that saves async changes:
public async Task<int> SaveOrUpdateAsync<TEntity>(TEntity entity) where TEntity : class, IContextEntity { if (entity.Id == 0) context.Set<TEntity>().Add(entity); else { TEntity dbEntry = context.Set<TEntity>().Find(entity.Id); if (dbEntry != null) dbEntry = entity; } return await context.SaveChangesAsync(); } public void Save() { Task saveEntit1Async = repository.SaveOrUpdateAsync<Entity1>(entity1); Task saveEntity2Async = repository.SaveOrUpdateAsync<Entity2>(entity2); Task saveEntity3Async = repository.SaveOrUpdateAsync<Entity3>(Entity3); Task.WaitAll(saveEntit1Async, saveEntity2Async, saveEntity3Async); string test = "test"; )
Call stuck on
Task.WaitAll(saveEntit1Async, saveEntity2Async, saveEntity3Async);
and never gets into
string test = "test";
But if I ran it like:
public void Save() { repository.SaveOrUpdateAsync<Entity1>(entity1); repository.SaveOrUpdateAsync<Entity2>(entity2); repository.SaveOrUpdateAsync<Entity3>(Entity3); string test = "test"; )
It works great, all changes are saved and reach
string test = "test";
Why
Task.WaitAll(saveEntit1Async, saveEntity2Async, saveEntity3Async);
It freezes the operation and never passes the call to the next line of code (line test = "test";)?
c # asynchronous entity-framework
Alexander C.
source share