public async Task SaveThumbnail(int id, string fileName, IDataContent content) { try { TransactionOptions topts = new TransactionOptions() { Timeout = TimeSpan.FromSeconds(60), IsolationLevel = IsolationLevel.Serializable }; using (TransactionScope trxScope = new TransactionScope(TransactionScopeOption.Required, topts, TransactionScopeAsyncFlowOption.Enabled)) using (System.Data.Common.DbConnection conn = _connectionFactory.GetRIAppDemoConnection()) { DbContextOptionsBuilder <AdventureWorksLT2012Context> dbOptionsBuilder = new DbContextOptionsBuilder <AdventureWorksLT2012Context>(); dbOptionsBuilder.UseSqlServer(conn); // Create in the same transaction !!! using (AdventureWorksLT2012Context db = new AdventureWorksLT2012Context(dbOptionsBuilder.Options)) { Product product = await db.Product.Where(a => a.ProductId == id).FirstOrDefaultAsync(); if (product == null) { throw new Exception(string.Format("Product {0} is Not Found", id)); } using (BlobStream blobStream = new BlobStream(conn as SqlConnection, "[SalesLT].[Product]", "ThumbNailPhoto", string.Format("WHERE [ProductID]={0}", id))) using (BufferedStream bufferedStream = new BufferedStream(blobStream, 128 * 1024)) { await blobStream.InitColumnAsync(); blobStream.Open(); Task delayTask = Task.Delay(TimeSpan.FromSeconds(15)); Task completedTask = await Task.WhenAny(content.CopyToAsync(bufferedStream), delayTask); if (completedTask == delayTask) { throw new Exception("Saving Image took longer than expected"); } await bufferedStream.FlushAsync(); } product.ThumbnailPhotoFileName = fileName; await db.SaveChangesAsync(); trxScope.Complete(); } } } catch (Exception ex) { string msg = ""; if (ex != null) { msg = ex.GetFullMessage(); } _logger.LogError(ex, msg); throw; } }