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 void DeleteObject <T>(T obj) where T : class { using (CarReservationContext context = new CarReservationContext()) { context.Entry <T>(obj).State = EntityState.Deleted; context.SaveChanges(); } }
private void SeedTestData() { Car[] cars = { new StandardCar { Make = "Fiat Punto", DailyRate = 50 }, new MidClassCar() { Make = "VW Golf", DailyRate = 120 }, new LuxuryClassCar() { Make = "Audi S6", DailyRate = 180, BaseRate = 50 }, new StandardCar() { Make = "Fiat 500", DailyRate = 75 }, }; Customer[] customers = { new Customer { LastName = "Nass", FirstName = "Anna", BirthDate = new DateTime(1981, 05, 05) }, new Customer { LastName = "Beil", FirstName = "Timo", BirthDate = new DateTime(1980, 09, 09) }, new Customer { LastName = "Pfahl", FirstName = "Martha", BirthDate = new DateTime(1990, 07, 03) }, new Customer { LastName = "Zufall", FirstName = "Rainer", BirthDate = new DateTime(1954, 11, 11) }, }; int year = DateTime.Now.Year + 1; Reservation[] reservations = { new Reservation { Car = cars[0], Customer = customers[0], From = new DateTime(year, 01, 10), To = new DateTime(year, 01, 20) }, new Reservation { Car = cars[1], Customer = customers[1], From = new DateTime(year, 01, 10), To = new DateTime(year, 01, 20) }, new Reservation { Car = cars[2], Customer = customers[2], From = new DateTime(year, 01, 10), To = new DateTime(year, 01, 20) }, new Reservation { Car = cars[1], Customer = customers[0], From = new DateTime(year, 05, 19), To = new DateTime(year, 06, 19) }, }; Context.Cars.AddRange(cars); Context.Customers.AddRange(customers); Context.Reservations.AddRange(reservations); Context.SaveChanges(); }
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); } } }