Transaction scope

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi!
I have a task to upload some data and update information for products.
So i have a code like this:

            IEnumerable<DataRow> q = from p in _odbcConnector.readData(strSql.ToString()).Tables[0].AsEnumerable() select p ;
            q.ToList().ForEach(p => UpdateProductInfo(Convert.ToInt32(p.ItemArray[0])));


After this i have a method to update product info like

public void UpdateProductInfo(int msgId)
{
var product = _productService.GetProductBySku(row.ItemArray[0].ToString());
if(product != null)
{
something....
}
}



Question: I want to do it in a one transaction scope. So Update or not full list of products.
So question is how to do it in a nope?
6 years ago
Check out this


var dbContext = EngineContext.Current.Resolve<IDbContext>() as DbContext;
if (dbContext != null)
{
    // We can use transaction scope
    using (var transaction = dbContext.Database.BeginTransaction())
    {
        try
        {
            // Do your stuff inside transaction scope
            transaction.Commit();
        }
        catch
        {
            transaction.Rollback();
            throw;
        }
    }
}
else
{
    // Do your stuff outside transaction scope
    // Fallback
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.