public async Task <TextBlob> AddText(string text) { if (text == null) { return new TextBlob { Hash = null, Text = null } } ; if (text.Length > MaxTextSize) { text = text.Substring(0, MaxTextSize); } var hash = GetHash(text); var blob = db.Texts.Find(hash); if (blob != null) { return(blob); } blob = new TextBlob { Hash = hash, Text = text }; db.Texts.AddOrUpdate(blob); try { await db.SaveChangesAsync(); } catch (DbEntityValidationException e) { throw new Exception( string.Join("\r\n", e.EntityValidationErrors.SelectMany(v => v.ValidationErrors).Select(err => err.PropertyName + " " + err.ErrorMessage))); } catch (DbUpdateException) { // It's ok, just tried to insert text with hash which already exists, try to find it if (!db.Texts.AsNoTracking().Any(t => t.Hash == hash)) { throw; } db.Entry(blob).State = EntityState.Unchanged; } return(blob); }
public async Task RemoveWaitingManualExerciseCheckings(string courseId, Guid slideId, string userId) { using (var transaction = db.Database.BeginTransaction()) { var checkings = GetSlideCheckingsByUser <ManualExerciseChecking>(courseId, slideId, userId, false).Where(c => !c.IsChecked && !c.IsLocked); foreach (var checking in checkings) { // Use EntityState.Deleted because EF could don't know abount these checkings (they have been retrieved via AsNoTracking()) db.Entry(checking).State = EntityState.Deleted; } await db.SaveChangesAsync(); transaction.Commit(); } }