private void AddLabel(DTO.LabelDTO label, DatabaseContext dbContext) { var dbLabel = dbContext.Labels .AsTracking() .AsEnumerable() .FirstOrDefault(l => (l.LabelId == label.Id || string.Equals(l.Name, label.Name, StringComparison.CurrentCultureIgnoreCase))); if (dbLabel == null) { throw new Exception($"Failed to find label with id {label.Id} or name '{label.Name}'."); } if (NoteLabels.Any(nl => nl.LabelId == dbLabel.LabelId)) { throw new Exception($"Duplicate labels found with name '{dbLabel.Name}'"); } NoteLabels.Add(new NoteLabel() { Note = this, Label = dbLabel }); }
public void SetFrom(DTO.NoteDTO note, DatabaseContext dbContext) { Edited = DateTime.Now; Title = note.Title; Body = note.Body; Category = note.Category; NoteImages.Clear(); foreach (DTO.ImageDTO image in note.Images) { NoteImages.Add(new Image(image)); } // Repopulate note labels NoteLabels.Clear(); foreach (DTO.LabelDTO label in note.Labels) { AddLabel(label, dbContext); } }