private static void UpdateRecord() // Обновление записи { using (var entities = new AutoLotEntities()) { Car carToUpdate = entities.Cars.Find(2222); if (carToUpdate != null) { carToUpdate.Color = "Blue"; entities.SaveChanges(); } } }
private static void RemoveRecord() // Удаление записи { using (var entities = new AutoLotEntities()) { // EntityKey key = new EntityKey("AutoLotEntities.Cars", "CarId", 2222); Car carToDelete = entities.Cars.Find(2222); // Поиск по ключу для искомой сущности if (carToDelete != null) { entities.Cars.Remove(carToDelete); entities.SaveChanges(); } } }
private static void RemoveRecordWithLinq() // Удаление записи через LINQ { using (var entities = new AutoLotEntities()) { var carToDelete = (from car in entities.Cars where car.CarId == 2222 select car).FirstOrDefault(); if (carToDelete != null) { entities.Cars.Remove(carToDelete); entities.SaveChanges(); } } }
private static void AddNewRecord() // Добавление новой записи { using (var context = new AutoLotEntities()) { try { context.Cars.Add(new Car() { CarId = 3333, Make = "Yugo", Color = "Brown" }); context.SaveChanges(); } catch (Exception exception) { Console.WriteLine(exception.InnerException.Message); } } }