public void InsertUsTreasuryRealLongTermRate(List <TreasuryRealLongTermRate> treasuryRealLongTermRates) { using (var scope = _dbContextScopeFactory.CreateWithTransaction(IsolationLevel.ReadUncommitted)) { var assetProduct = _assetProductRepository.FindAll().Single(x => x.Name.Equals("US Treasury")); var usTreasuryDatas = _usTreasuryDataRepository.FindAll().Where(x => x.AssetProduct.Id == assetProduct.Id).ToList(); var usTreasuryCache = new ConcurrentDictionary <DateTime, UsTreasuryData>(); foreach (var treasuryData in usTreasuryDatas) { usTreasuryCache[treasuryData.Timestamp] = treasuryData; } foreach (var treasuryRealLongTermRate in treasuryRealLongTermRates) { if (usTreasuryCache.ContainsKey(treasuryRealLongTermRate.Timestamp)) { continue; } var usTreasuryData = new UsTreasuryData { Timestamp = treasuryRealLongTermRate.Timestamp, AssetProduct = assetProduct, LongTermRealAverageRate = treasuryRealLongTermRate.LongTermRealAverageRate }; _usTreasuryDataRepository.Insert(usTreasuryData); } scope.SaveChanges(); } }
public ActionResult Save() { var province = new Province() { Population = 3000000, Name = "BeiJing", }; var province1 = new Province() { Population = 1000000, Name = "jiangsu", }; var company = new Company() { Name = "Augumentum", EstablishDate = DateTime.Now, LegalPerson = "zack", }; using (var dbScope = _dbContextScopeFactory.CreateWithTransaction(IsolationLevel.ReadCommitted)) { _provinceRepository.BulkInsert(new List <Province> { province, province1 }); dbScope.SaveChanges(); } return(Ok()); }
/// <summary> /// Forces the creation of a new ambient DbContextScope (i.e. does not /// join the ambient scope if there is one) and wraps all DbContext instances /// created within that scope in an explicit database transaction with /// the provided isolation level. /// WARNING: the database transaction will remain open for the whole /// duration of the scope! So keep the scope as short-lived as possible. /// Don't make any remote API calls or perform any long running computation /// within that scope. /// This is an advanced feature that you should use very carefully /// and only if you fully understand the implications of doing this. /// </summary> public static TDbContext Open <TDbContext>(this IDbContextScopeFactory self, IsolationLevel isolationLevel) where TDbContext : DbContext { var dbContextScope = self.CreateWithTransaction(isolationLevel); return(dbContextScope.Get <TDbContext>()); }
public IWynnDbContextScope CreateWithReadCommittedTransaction(string transactionUser) { var dbContextScope = _dbContextScopeFactory.CreateWithTransaction(IsolationLevel.ReadCommitted); return(new WynnDbContextScope(dbContextScope, _dbContextLocator)); }
public ResponseHelper Purchase(int courseId, string userId) { var rh = new ResponseHelper(); try { using (var ctx = _dbContextScopeFactory.CreateWithTransaction( IsolationLevel.Serializable )) { var user = _applicationUserRepo.Single(x => x.Id == userId); var course = _courseRepo.Single(x => x.Id == courseId); var hasTaken = _userPerCourseRepo.Find(x => x.UserId == userId && x.CourseId == courseId ).Any(); // Si el curso ya fue tomado no hacemos nada if (hasTaken) { rh.SetResponse(false, "El curso ya ha sido tomado por este alumno"); } // Si el precio del curso es mayor a los créditos actuales else if (course.Price > user.Credit) { rh.SetResponse(false, "Usted no cuenta con los créditos necesarios para adquirir el curso"); } else { var hasPriorCourses = _userPerCourseRepo.Find(x => x.UserId == userId ).Any(); // Verificamos si es la primera vez que compra el curso. Si fuera así, asignamos el rol de estudiante if (!hasPriorCourses) { var role = _roleRepo.Single(x => x.Name == RoleNames.Student ); _applicationUserRole.Insert(new ApplicationUserRole { UserId = userId, RoleId = role.Id }); } // Asignamos un curso a un usuario _userPerCourseRepo.Insert(new UsersPerCourse { UserId = userId, CourseId = courseId }); // Actualizamos el crédito del usuario user.Credit = user.Credit - course.Price; _applicationUserRepo.Update(user); var incomes = new List <Income> { // Ingreso total new Income { EntityType = Enums.EntityType.Courses, EntityID = course.Id, IncomeType = Enums.IncomeType.Total, Total = course.Price }, // Ingreso profesor new Income { EntityType = Enums.EntityType.Courses, EntityID = course.Id, IncomeType = Enums.IncomeType.TeacherTotal, Total = course.Price * (Convert.ToDecimal(Parameters.TeacherComission) / 100) }, // Ingreso para la empresa new Income { EntityType = Enums.EntityType.Courses, EntityID = course.Id, IncomeType = Enums.IncomeType.CompanyTotal, Total = course.Price * (Convert.ToDecimal(Parameters.SalesCommission) / 100) } }; _incomeRepo.Insert(incomes); rh.SetResponse(true); } ctx.SaveChanges(); } } catch (Exception e) { logger.Error(e.Message); rh.SetResponse(false, e.Message); } return(rh); }