public List <Reservation> GetAllReservation() { using (CarReservationContext context = new CarReservationContext()) { return(context.Reservations.Include(r => r.Customer).Include(r => r.Car).ToList()); } }
public T GetElement <T>(Func <T, bool> predicateFunc) where T : class { using (CarReservationContext context = new CarReservationContext()) { return(context.Set <T>().SingleOrDefault(predicateFunc)); } }
public async Task <Reservation> Insert(Reservation reservation) { using (CarReservationContext context = new CarReservationContext()) { if (!DateRangeCheck(reservation)) { throw new InvalidDateRangeException( $"Reservation < 24h -> {(reservation.From - reservation.To).TotalHours}h"); } if (!await AvailabilityCheck(reservation)) { throw new CarUnavailableException($"car: {reservation.CarId} unavailable"); } context.Entry(reservation).State = EntityState.Added; await context.SaveChangesAsync(); reservation.Car = await context.Cars.FindAsync(reservation.CarId); await context.Entry(reservation).Reference(r => r.Customer).LoadAsync(); return(reservation); } }
public Reservation GetReservationById(int id) { using (CarReservationContext context = new CarReservationContext()) { return(context.Reservations.Include(r => r.Customer).Include(r => r.Car).SingleOrDefault(r => r.ReservationNo == id)); } }
public List <T> GetAll <T>() where T : class { using (CarReservationContext context = new CarReservationContext()) { return(context.Set <T>().ToList()); } }
public T SaveObject <T>(T obj, int id, bool isNew) where T : class { using (CarReservationContext context = new CarReservationContext()) { context.Database.Log = Console.Write; try { context.Entry <T>(obj).State = isNew ? EntityState.Added : EntityState.Modified; context.SaveChanges(); if (!isNew && typeof(T).Name == "Car") { context.Database.ExecuteSqlCommand("UPDATE cars SET CarClass = {0} WHERE Id = {1}", obj.GetType().Name == "StandardCar" ? 2: (obj.GetType().Name == "MidRangeCar"? 1 : 0), id); context.SaveChanges(); } return(obj); } catch (DbUpdateConcurrencyException ex) { ex.Entries.SingleOrDefault().Reload(); throw CreateLocalOptimisticConcurrencyException(context, obj); } } }
public async Task Delete(Customer customer) { using (CarReservationContext context = new CarReservationContext()) { context.Entry(customer).State = EntityState.Deleted; await context.SaveChangesAsync(); } }
public void DeleteObject <T>(T obj) where T : class { using (CarReservationContext context = new CarReservationContext()) { context.Entry <T>(obj).State = EntityState.Deleted; context.SaveChanges(); } }
protected static OptimisticConcurrencyException <T> CreateOptimisticConcurrencyException <T>( CarReservationContext context, T entity) where T : class { T dbEntity = (T)context.Entry(entity) .GetDatabaseValues() .ToObject(); return(new OptimisticConcurrencyException <T>($"Update {typeof(T).Name}: Concurrency-Fehler", dbEntity)); }
public async Task <Customer> Insert(Customer customer) { using (CarReservationContext context = new CarReservationContext()) { context.Entry(customer).State = EntityState.Added; await context.SaveChangesAsync(); return(customer); } }
public async Task <Customer> Get(int primaryKey) { using CarReservationContext context = new CarReservationContext(); try { return(await context.Customers.Include(c => c.Reservations).SingleAsync(c => c.Id == primaryKey)); } catch (Exception ex) { throw new KeyNotFoundException(ex.Message, ex); } }
public async Task <Reservation> Get(int primaryKey) { try { using CarReservationContext context = new CarReservationContext(); return(await context.Reservations.Include(r => r.Car).Include(r => r.Customer) .SingleAsync(c => c.ReservationNr == primaryKey)); } catch (Exception ex) { throw new KeyNotFoundException(ex.Message, ex); } }
public async Task Update(Customer customer) { using (CarReservationContext context = new CarReservationContext()) { try { context.Entry(customer).State = EntityState.Modified; await context.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e.Message); throw CreateOptimisticConcurrencyException(context, customer); } } }
public async Task Update(Reservation reservation) { using (CarReservationContext context = new CarReservationContext()) { if (!DateRangeCheck(reservation)) { throw new InvalidDateRangeException( $"Reservation < 24h -> {(reservation.From - reservation.To).TotalHours}h"); } if (!await AvailabilityCheck(reservation)) { throw new CarUnavailableException($"car: {reservation.CarId} unavailable"); } context.Entry(reservation).State = EntityState.Modified; await context.SaveChangesAsync(); } }
/// <summary> /// This method initializes the test environment including the database. /// </summary> private void InitializeTestEnvironment() { using CarReservationContext context = new CarReservationContext(); TestDataHelper testDataHelper; if (context.Database.IsInMemory()) { testDataHelper = new TestDataHelperInMemory(context); } else if (context.Database.IsSqlServer()) { testDataHelper = new TestDataHelperSqlServer(context); } else { throw new NotImplementedException(); } testDataHelper.InitializeTestData(); }
public async Task <List <Customer> > GetAll() { using CarReservationContext context = new CarReservationContext(); return(await context.Customers.Include(c => c.Reservations).ToListAsync()); }
public TestDataHelperInMemory(CarReservationContext context) : base(context) { }
protected TestDataHelper(CarReservationContext context) { Context = context; }
public static void InitializeTestData() { using (CarReservationContext context = new CarReservationContext()) { var luxuryCarTableName = context.GetTableName <LuxuryCar>(); var midRangeCarTableName = context.GetTableName <MidRangeCar>(); var standardCarTableName = context.GetTableName <StandardCar>(); var carTableName = context.GetTableName <Car>(); var customerTableName = context.GetTableName <Customer>(); var reservationTableName = context.GetTableName <Reservation>(); try { context.Database.Log = Console.Write; // Delete all records from tables // > Cleanup for specific subtypes necessary when not using table per hierarchy (TPH) // since entities will be stored in different tables. if (luxuryCarTableName != carTableName) { context.DeleteAllRecords(luxuryCarTableName); } if (midRangeCarTableName != carTableName) { context.DeleteAllRecords(midRangeCarTableName); } if (standardCarTableName != carTableName) { context.DeleteAllRecords(standardCarTableName); } context.DeleteAllRecords(reservationTableName); context.DeleteAllRecords(carTableName); context.DeleteAllRecords(customerTableName); // Reset the identity seed (Id's will start again from 1) context.ResetEntitySeed(luxuryCarTableName); context.ResetEntitySeed(midRangeCarTableName); context.ResetEntitySeed(standardCarTableName); context.ResetEntitySeed(carTableName); context.ResetEntitySeed(customerTableName); context.ResetEntitySeed(reservationTableName); // Temporarily allow insertion of identity columns (Id) context.SetAutoIncrementOnTable(luxuryCarTableName, true); context.SetAutoIncrementOnTable(midRangeCarTableName, true); context.SetAutoIncrementOnTable(standardCarTableName, true); context.SetAutoIncrementOnTable(carTableName, true); context.SetAutoIncrementOnTable(customerTableName, true); context.SetAutoIncrementOnTable(reservationTableName, true); // Insert test data context.Cars.AddRange(Cars); context.Customers.AddRange(Customers); context.Reservations.AddRange(Reservations); context.SaveChanges(); } catch (Exception ex) { throw new ApplicationException("Error while re-initializing database entries.", ex); } finally { // Disable insertion of identity columns (Id) context.SetAutoIncrementOnTable(luxuryCarTableName, false); context.SetAutoIncrementOnTable(midRangeCarTableName, false); context.SetAutoIncrementOnTable(standardCarTableName, false); context.SetAutoIncrementOnTable(carTableName, false); context.SetAutoIncrementOnTable(customerTableName, false); context.SetAutoIncrementOnTable(reservationTableName, false); } } }
public async Task <List <Reservation> > GetAll() { using CarReservationContext context = new CarReservationContext(); return(await context.Reservations.Include(r => r.Car).Include(r => r.Customer).ToListAsync()); }
public TestDataHelperSqlServer(CarReservationContext context) : base(context) { }