private void TrySaveNote(Note note) { var dbNote = _context.Notes.Include(n => n.Categories).SingleOrDefault(n => n.NoteID == note.NoteID) ?? note; dbNote.Title = note.Title; dbNote.IsMarkdownFile = note.IsMarkdownFile; dbNote.Description = note.Description; dbNote.NoteDate = note.NoteDate; var categories = note.Categories.ToList(); note.Categories.Clear(); foreach (var category in categories) { note.Categories.Add(new NoteCategory { NoteID = note.NoteID, Category = _context.Categories.Find(category.CategoryID) }); } dbNote.Categories = note.Categories; _context.Entry(dbNote).Property("RowVersion").OriginalValue = note.RowVersion; _context.Update(dbNote); _context.SaveChanges(); }