public async Task RemoveWaitingManualExerciseCheckings(string courseId, Guid slideId, string userId) { using (var transaction = db.Database.BeginTransaction()) { var checkings = GetSlideCheckingsByUser <ManualExerciseChecking>(courseId, slideId, userId, noTracking: false) .AsEnumerable() .Where(c => !c.IsChecked && !c.IsLocked) .ToList(); foreach (var checking in checkings) { // Use EntityState.Deleted because EF could don't know about these checkings (they have been retrieved via AsNoTracking()) // TODO (andgein): Now it's not retrieved via AsNoTracking(). Fix this. foreach (var review in checking.Reviews.ToList()) { db.Entry(review).State = EntityState.Deleted; } db.Entry(checking).State = EntityState.Deleted; } await db.SaveChangesAsync().ConfigureAwait(false); transaction.Commit(); } }
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); } if (text.Contains('\0')) { text = text.Replace("\0", ""); // postgres не поддерживает \0 в строках } var hash = GetHash(text); var blob = await db.Texts.FindAsync(hash); if (blob != null) { return(blob); } blob = new TextBlob { Hash = hash, Text = text }; db.AddOrUpdate(blob, b => b.Hash == hash); try { await db.SaveChangesAsync().ConfigureAwait(false); } 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 <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.AddOrUpdate(blob, b => b.Hash == hash); try { await db.SaveChangesAsync(); } 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); }