private static void DetachTest() { // deserializacja var customer = new Customer { CustomerId = 1, FirstName = "Bartek", LastName = "Sulecki", Identifier = "47437373", PhoneNumber = "555-666-777", Email = "*****@*****.**", }; using (var context = new RentalBikesContext()) { // zła praktyka! //var cust = context.Customers // .SingleOrDefault(d => d.CustomerId == customer.CustomerId); //cust.Email = "*****@*****.**"; //var customer = context.Customers // .AsNoTracking() // .First(p => p.Identifier == "848484730"); Console.WriteLine(context.Entry(customer).State); // context.Customers.Add(customer); context.Customers.Attach(customer); // modyfikacja całego obiektu // context.Entry(customer).State = System.Data.Entity.EntityState.Modified; // modyfikacja wybranych pól context.Entry(customer).Property(p => p.PhoneNumber).IsModified = true; context.Entry(customer).Property(p => p.Email).IsModified = true; Console.WriteLine(context.Entry(customer).State); context.SaveChanges(); } }
private static void AddRentalTest() { using (var context = new RentalBikesContext()) { var customer = context.Customers .FirstOrDefault(c => c.Identifier == "848484730"); Console.WriteLine(context.Entry(customer).State); var bike = context.Vehicles.OfType <Bike>().First(); var station = context.Stations.Single(s => s.Symbol == "ST001"); var rental = new Rental { Rentee = customer, Bike = bike, FromStation = station, }; context.Rentals.Add(rental); Console.WriteLine(context.Entry(rental).State); context.SaveChanges(); //var lastBike = context.Vehicles.OfType<Bike>() // .OrderBy(b=>b.VehicleId) // .Last(); //rental.Bike = lastBike; context.SaveChanges(); Console.WriteLine(context.Entry(rental).State); } }
private static void AddTest() { using (var context = new RentalBikesContext()) { var customer = new Customer { FirstName = "Bartek", LastName = "Sulecki", Identifier = "47437373", PhoneNumber = "555-666-777" }; Console.WriteLine(context.Entry(customer).State); context.Customers.Add(customer); Console.WriteLine(context.Entry(customer).State); context.SaveChanges(); Console.WriteLine(context.Entry(customer).State); customer.PhoneNumber = "444-555-666"; customer.Identifier = "99999999"; Console.WriteLine(context.Entry(customer).State); context.SaveChanges(); Console.WriteLine(context.Entry(customer).State); context.Customers.Remove(customer); Console.WriteLine(context.Entry(customer).State); context.SaveChanges(); Console.WriteLine(context.Entry(customer).State); } }
private static void AddRentalTest2() { var rental = new Rental { Rentee = new Customer { CustomerId = 1, FirstName = "Marcin", LastName = "Sulecki" }, Bike = new Bike { VehicleId = 1, BikeType = BikeType.Town, Color = "Green" }, FromStation = new Station { StationId = 3, Name = "Altkom", Address = "Chłodna", }, ToStation = new Station { StationId = 3, Name = "Altkom", Address = "Chłodna", }, CreateDate = DateTime.Now, }; using (var context = new RentalBikesContext()) { context.Rentals.Add(rental); Console.WriteLine(context.Entry(rental).State); context.Entry(rental.Rentee).State = System.Data.Entity.EntityState.Unchanged; Console.WriteLine(context.Entry(rental.Rentee).State); context.Entry(rental.Bike).State = System.Data.Entity.EntityState.Unchanged; Console.WriteLine(context.Entry(rental.Bike).State); context.Entry(rental.FromStation).State = System.Data.Entity.EntityState.Unchanged; Console.WriteLine(context.Entry(rental.FromStation).State); context.Entry(rental.ToStation).State = System.Data.Entity.EntityState.Unchanged; Console.WriteLine(context.Entry(rental.ToStation).State); Console.WriteLine(context.Entry(rental).State); context.SaveChanges(); } }