public ActionResult <string> AddNote(Note note) { try { ValidateNote(note); var categories = note.Categories.ToList(); note.Categories.Clear(); _context.Attach(note); foreach (var category in categories) { note.Categories.Add(new NoteCategory { NoteID = note.NoteID, Category = _context.Categories.Find(category.CategoryID) }); } _context.Add(note); _context.SaveChanges(); return(new ActionResult <string>("Note added.")); }catch (Exception e) { return(BadRequest()); } }