// Update or create product public void SaveMeal(Meal meal) { // Meal does not exist -> create it if (meal.Id == 0) { database.Meals.Add(meal); } // Meal does exits -> update it else { Meal dbEntry = database.Meals.FirstOrDefault(m => m.Id == meal.Id); if (dbEntry != null) { dbEntry.Name = meal.Name; dbEntry.Description = meal.Description; dbEntry.Price = meal.Price; dbEntry.MaxGuests = meal.MaxGuests; } } // Save changes database.SaveChanges(); }
// Stores a student public void SaveStudent(Student student) { // If id = 0 -> student does not exist -> create it if (student.Id == 0) { // Create student database.Students.Add(student); } else // If id = not 0 -> student exists -> update it // Get student { Student dbEntry = database.Students.FirstOrDefault(m => m.Id == student.Id); // Update properties if (dbEntry != null) { dbEntry.Name = student.Name; dbEntry.UserId = student.UserId; } } // Save changes database.SaveChanges(); }