Base solution for your next web application
Open Closed

SQL Transaction #8876


User avatar
0
mahendra created

Hi,

In AppService I am doing the following steps

  1. Start UOW
  2. First Insert using InsertAsync
  3. Second Insert using InsertAsync
  4. a SP called using _dbContextProvider.GetDbContext().Database.ExecuteSqlRawAsync(sqlQuery);
  5. 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)
  • User Avatar
    0
    BobIngham created

    The sp is fired outside of the UoW context and therefore fires first. Try:

    1. Start UOW
    2. First Insert using InsertAsync
    3. Second Insert using InsertAsync
    4. UOW SaveChanges.
    5. 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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.