Hi,
In AppService I am doing the following steps
- Start UOW
- First Insert using InsertAsync
- Second Insert using InsertAsync
- a SP called using _dbContextProvider.GetDbContext().Database.ExecuteSqlRawAsync(sqlQuery);
- and finally UOW SaveChanges.
Now if a see the SQL profiler, the query (essentially a SP) that I am executing in step 4 gets executed first instead of last. What mistake I am doing and how can I ensure the execution sequentially. Essentially, on the 4th step, I am trying to execute a stored procedure but this procedure should be executed only after step2 and step3 and all the three steps in a single transaction.
Please help....
2 Answer(s)
-
0
The sp is fired outside of the UoW context and therefore fires first. Try:
- Start UOW
- First Insert using InsertAsync
- Second Insert using InsertAsync
- UOW SaveChanges.
- and finally a SP called using _dbContextProvider.GetDbContext().Database.ExecuteSqlRawAsync(sqlQuery);
There are some suggestions here: how-can-i-use-a-stored-procedure-repository-unit-of-work-patterns-in-entity-framework to include the sp within the UoW.
-
0
Thanks @bobingham :)
@mahendra, if you don't call UOW SaveChanges in step 4 explained by @bobingham, all inserts will be executed when app service method is completed. That is where uow.savechanges is called automatically.
For SP, it is executed right away.