public ICategory Save(ICategory category) { using (var context = new NoteTakingContext(_connectionString)) { context.Database.EnsureCreated(); var mySqlCategory = _mapper.Map <MySqlCategory>(category); context.Category.Attach(mySqlCategory); foreach (var note in mySqlCategory.Notes) { if (context.Note.Any(x => x.Id.Equals(note.Id))) { context.Note.Update(note); } else { context.Note.Add(note); } } context.SaveChanges(); return(category); } }
public void Delete(Guid id) { using (var context = new NoteTakingContext(_connectionString)) { context.Database.EnsureCreated(); var toRemove = context.Category.FirstOrDefault(x => x.Id == id); context.Remove(toRemove); context.SaveChanges(); } }
public ICategory Add(ICategory category) { using (var context = new NoteTakingContext(_connectionString)) { context.Database.EnsureCreated(); var mySqlCategory = _mapper.Map <MySqlCategory>(category); context.Category.Add(mySqlCategory); context.SaveChanges(); return(_mapper.Map <Category>(mySqlCategory));; } }