public static void EditVehicle(int id, IVehicle vehicle) { if (vehicle.AllAttributesNull()) { throw new AllAttributesNullException("None of the attributes in the vehicle had a value"); } var builder = new DbContextOptionsBuilder <VehicleRecordDBContext>(); builder.UseInMemoryDatabase("VehicleRecordDB"); using (DAL.VehicleRecordDBContext database = new DAL.VehicleRecordDBContext(builder.Options)) { Vehicle dbVehicle = database.Vehicles.Where(v => v.Id == id).FirstOrDefault(); if (dbVehicle == null) { throw new VehicleNotFoundException("The vehicle with id " + id.ToString() + " was not found"); } dbVehicle.Make = vehicle.Make; dbVehicle.Model = vehicle.Model; if ((VehicleType)dbVehicle.VehicleType == VehicleType.Car) { Car car = (Car)vehicle; dbVehicle.Engine = car.Engine; dbVehicle.BodyType = car.BodyType; dbVehicle.Wheels = car.Wheels; dbVehicle.Doors = car.Doors; } database.SaveChanges(); } }
public static void CreateVehicle(IVehicle vehicle) { Vehicle dbVehicle = null; if (vehicle.AllAttributesNull()) { throw new AllAttributesNullException("None of the attributes in the vehicle had a value"); } if (vehicle.GetType() == typeof(Car)) { dbVehicle = Car.ConvertToDbVehicle((Car)vehicle); } if (dbVehicle != null) { var builder = new DbContextOptionsBuilder <VehicleRecordDBContext>(); builder.UseInMemoryDatabase("VehicleRecordDB"); using (DAL.VehicleRecordDBContext database = new DAL.VehicleRecordDBContext(builder.Options)) { database.Vehicles.Add(dbVehicle); database.SaveChanges(); } } }