예제 #1
0
 public int Delete(int id, byte[] timeStamp)
 {
     _db.Entry(new T {
         Id = id, TimeStamp = timeStamp
     }).State = EntityState.Deleted;
     return(SaveChanges());
 }
예제 #2
0
 public int Delete(int id, byte[] timeStamp)
 {
     ctx.Entry <T>(new T()
     {
         Id = id, Timestamp = timeStamp
     }).State = EntityState.Deleted;
     return(SaveChanges());
 }
예제 #3
0
 private static void PrintAllInventory()
 {
     // Select all items from the Inventory table of AutoLot,
     // and print out the data using our custom ToString()
     // of the Car entity class.
     using (var context = new AutoLotEntities())
     {
         foreach (Car c in context.Cars)
         {
             WriteLine(c);
         }
         foreach (Car c in context.Cars.SqlQuery(
                      "Select CarId,Make,Color,PetName as CarNickName from Inventory where Make=@p0", "BMW"))
         {
             WriteLine(c);
         }
         foreach (ShortCar c in context.Database.SqlQuery(typeof(ShortCar), "Select CarId,Make from Inventory"))
         {
             WriteLine(c);
         }
         foreach (Car c in context.Cars.Where(c => c.Make == "BMW"))
         {
             WriteLine(c);
         }
         WriteLine(context.Cars.Find(5));
         foreach (Car c in context.Cars)
         {
             foreach (Order o in c.Orders)
             {
                 WriteLine(o.OrderId);
             }
         }
         foreach (Car c in context.Cars.Include(c => c.Orders))
         {
             foreach (Order o in c.Orders)
             {
                 WriteLine(o.OrderId);
             }
         }
         context.Configuration.LazyLoadingEnabled = false;
         foreach (Car c in context.Cars)
         {
             context.Entry(c).Collection(x => x.Orders).Load();
             foreach (Order o in c.Orders)
             {
                 WriteLine(o.OrderId);
             }
         }
         foreach (Order o in context.Orders)
         {
             context.Entry(o).Reference(x => x.Car).Load();
         }
     }
 }
예제 #4
0
 public static void UpdateCarRecord(int carId)
 {
     using (AutoLotEntities context = new AutoLotEntities()) {
         Car carToUpdate = context.Cars.Find(carId);
         if (carToUpdate != null)
         {
             Console.WriteLine("State Before Update: {0}", context.Entry(carToUpdate).State);
             carToUpdate.PetName = "Sabrina";
             Console.WriteLine("State After Update: {0}", context.Entry(carToUpdate).State);
             context.SaveChanges();
         }
     }
 }
예제 #5
0
 private static void UpdateRecord(int carId)
 {
     using (var context = new AutoLotEntities())
     {
         var carToUpdate = context.Cars.Find(carId);
         if (carToUpdate != null)
         {
             Console.WriteLine(context.Entry(carToUpdate).State);
             carToUpdate.Color = "Blue";
             Console.WriteLine(context.Entry(carToUpdate).State);
             context.SaveChanges();
         }
     }
 }
예제 #6
0
 private static void UpdateRecord(int carId)
 {
     using (var context = new AutoLotEntities())
     {
         // Grab the car, change it, save!
         Car carToUpdate = context.Cars.Find(carId);
         if (carToUpdate != null)
         {
             WriteLine(context.Entry(carToUpdate).State);
             carToUpdate.Color = "Blue";
             WriteLine(context.Entry(carToUpdate).State);
         }
     }
 }
예제 #7
0
        private static void UpdateColor(int carId, string color)
        {
            WriteLine($"Update the Color to {color} for CarId {carId} ");

            using (var context = new AutoLotEntities()) {
                Car carToUpdate = context.cars.Find(carId);
                if (carToUpdate != null)
                {
                    WriteLine(context.Entry(carToUpdate).State);
                    carToUpdate.Color = color;
                    WriteLine(context.Entry(carToUpdate).State);
                    context.SaveChanges();
                }
            }
        }
예제 #8
0
 private static void UpdateRecord(int carld)
 {
     // Найти запись об автомобиле, подлежащую обновлению, по первичному ключу,
     using (var context = new AutoLotEntities())
     {
         // Получить запись об автомобиле, обновить ее и сохранить!
         Car carToUpdate = context.Cars.Find(carld);
         if (carToUpdate != null)
         {
             WriteLine(context.Entry(carToUpdate).State);
             carToUpdate.Color = "Blue";
             WriteLine(context.Entry(carToUpdate).State);
             context.SaveChanges();
         }
     }
 }
예제 #9
0
        /// <summary>
        /// Find a car by carId and then update it.
        /// </summary>
        /// <param name="carId"></param>
        private static void UpdateRecord(int carId)
        {
            using (var context = new AutoLotEntities())
            {
                // Grab the car, change it, save!
                Inventory carToUpdate = context.Inventories.Find(carId);

                WriteLine("UpdateRecord: Found " + carToUpdate);
                if (carToUpdate != null)
                {
                    WriteLine("Prior to update " + context.Entry(carToUpdate).State);
                    carToUpdate.Color = "Metallic Blue";
                    WriteLine("After update " + context.Entry(carToUpdate).State);
                    context.SaveChanges();
                }
            }
        }
예제 #10
0
 private static void GetAllCarOrdersExplicit()
 {
     using (var context = new AutoLotEntities())
     {
         context.Configuration.LazyLoadingEnabled = false;
         foreach (Car c in context.Cars)
         {
             context.Entry(c).Collection(x => x.Orders).Load();
             foreach (Order o in c.Orders)
             {
                 WriteLine(o.OrderId);
             }
         }
         foreach (Order o in context.Orders)
         {
             context.Entry(o).Reference(x => x.Car).Load();
         }
     }
 }
예제 #11
0
 public ActionResult Edit([Bind(Include = "CarId,Make,Color,PetName,Timestamp")] Inventory inventory)
 {
     if (ModelState.IsValid)
     {
         db.Entry(inventory).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(inventory));
 }
 static void UpdateRecord(int carId)
 {
     //Найти запись об автомобиле по первичному ключу
     using (var context = new AutoLotEntities())
     {
         Car carToUpdate = context.Cars.Find(carId);
         if (carToUpdate != null)
         {
             WriteLine(context.Entry(carToUpdate).State);
             carToUpdate.Color = "Red";
             WriteLine(context.Entry(carToUpdate).State);
             context.SaveChanges();
             foreach (var item in context.Cars)
             {
                 WriteLine(item);
             }
         }
     }
 }
예제 #13
0
 public ActionResult Edit([Bind(Include = "CustId,FirstName,LastName,Timestamp")] Customer customer)
 {
     if (ModelState.IsValid)
     {
         db.Entry(customer).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(customer));
 }
예제 #14
0
 public static void GetAllOrdersUsingExplicitLoading()
 {
     // efficient
     using (AutoLotEntities context = new AutoLotEntities()) {
         context.Configuration.LazyLoadingEnabled = false;
         foreach (Car car in context.Cars)
         {
             context.Entry(car).Collection("Orders").Load();
             foreach (Order order in car.Orders)
             {
                 Console.WriteLine(order);
             }
         }
         foreach (Order order in context.Orders)
         {
             context.Entry(order).Reference("Car").Load();
             Console.WriteLine(order.Car);
         }
     }
 }
        //Added a helper UpdateRecord(int carId) to update values in DB.
        private static void UpdateRecord(int carId)
        {
            using (var context = new AutoLotEntities())
            {
                //Find a specific car object which I want to delete, is found by primary key.
                Car carToUpdate = context.Cars.Find(carId);

                if (carToUpdate != null)
                {
                    //EntityState before update
                    WriteLine(context.Entry(carToUpdate).State);

                    //Set a new value.
                    carToUpdate.Color = "Blue";

                    //EntityState after update object's value in carToUpdate.Color
                    WriteLine(context.Entry(carToUpdate).State);

                    //Save changes to the DB.
                    context.SaveChanges();
                }
            }
        }
예제 #16
0
 private static void RemoveRecord(int carld)
 {
     using (var context = new AutoLotEntities())
     {
         Car carToDelete = context.Cars.Find(carld);
         if (carToDelete != null)
         {
             context.Cars.Remove(carToDelete);
             if (context.Entry(carToDelete).State != EntityState.Deleted)
             {
                 throw new Exception("Unable to delete the record");
             }
             context.SaveChanges();
         }
     }
 }
예제 #17
0
 private static void RemoveRecord(int carId)
 {
     // Find a car to delte by primary key
     using (var context = new AutoLotEntities())
     {
         Car carToDelete = context.Cars.Find(carId);
         if (carToDelete != null)
         {
             context.Cars.Remove(carToDelete);
             // This code is purely demonstrative to show the entity state changed to Deleted
             if (context.Entry(carToDelete).State != EntityState.Deleted)
             {
                 throw new Exception("Unable to delete the record");
             }
             context.SaveChanges();
         }
     }
 }
 static void RemoveRecordUsingEntityState(int carId)
 {
     using (var context = new AutoLotEntities())
     {
         Car carToDelete = new Car {
             CarId = carId
         };
         context.Entry(carToDelete).State = EntityState.Deleted;
         try
         {
             context.SaveChanges();
         }
         catch (DbUpdateConcurrencyException ex)
         {
             WriteLine(ex);
         }
     }
 }
예제 #19
0
 private static void RemoveRecordUsingEntityState(int carId)
 {
     using (var context = new AutoLotEntities())
     {
         Car carToDelete = new Car()
         {
             CarId = carId
         };                                                                         // create a new car entity, set CarId property with incoming param value
         context.Entry(carToDelete).State = System.Data.Entity.EntityState.Deleted; // set the state for this car in the context to 'deleted'
         try
         {
             context.SaveChanges();
         }
         catch (DbUpdateConcurrencyException ex)
         {
             WriteLine(ex);
         }
     }
 }
예제 #20
0
 public static void DeleteRecordByTracking(int carId)
 {
     using (AutoLotEntities context = new AutoLotEntities()) {
         Car car = context.Cars.Find(carId);
         if (car != null)
         {
             context.Cars.Remove(car);
         }
         if (context.Entry(car).State != EntityState.Deleted)
         {
             throw new Exception("Unable to delete record!");
         }
         else
         {
             Console.WriteLine("Deleted");
         }
         context.SaveChanges();
     }
 }
예제 #21
0
        private static void RemoveRecord(int carId)
        {
            using (var context = new AutoLotEntities())
            {
                Car carToDelete = context.Cars.Find(carId);
                if (carToDelete != null)
                {
                    context.Cars.Remove(carToDelete);
                    // Этот код предназначен чисто для демонстрации того,
                    // что состояние сущности изменилось на Deleted.
                    if (context.Entry(carToDelete).State != EntityState.Deleted)
                    {
                        throw new Exception("Unable to delete the record");
                    }

                    context.SaveChanges();
                }
            }
        }
예제 #22
0
        public static void DeleteRecordByEntityState(int carId)
        {
            // If an instance with the same primary key is already being tracked, this method will fail,
            // since you can’t have two of the same entities with the same primary key being tracked by
            // the DbChangeTracker.

            using (AutoLotEntities context = new AutoLotEntities()) {
                Car carToDelete = new Car()
                {
                    CarId = carId
                };
                context.Entry(carToDelete).State = EntityState.Deleted;
                try {
                    context.SaveChanges();
                    Console.WriteLine("Deleted.");
                } catch (DbUpdateConcurrencyException ex) {
                    Console.WriteLine(ex.Message);
                }
            }
        }
 static void RemoveRecord(int carId)
 {
     //Найти запись об автомобиле, подлежащую удалению, по первичному ключу.
     using (var context = new AutoLotEntities())
     {
         //Проверить наличие записи
         Car carToDelete = context.Cars.Find(carId);
         if (carToDelete != null)
         {
             context.Cars.Remove(carToDelete);
             //Этот код предназначен чисто для демонстрации того,
             //что состояние сущности изменилось на Deleted.
             if (context.Entry(carToDelete).State != EntityState.Deleted)
             {
                 throw new Exception("Unable to delete this record");
             }
             context.SaveChanges();
         }
     }
 }
예제 #24
0
        private static void ExplicitLoading()
        {
            ForegroundColor = ConsoleColor.Blue;
            WriteLine("-> Explicit loading:");

            using (var context = new AutoLotEntities()) {
                context.Configuration.LazyLoadingEnabled = false;

                foreach (Car c in context.cars)
                {
                    context.Entry(c).Collection(x => x.Orders).Load();

                    Write($"CarID-{c.CarId}, Count-{c.Orders.Count}");
                    foreach (Order o in c.Orders)
                    {
                        Write($" : <OrderID-{o.OrderId}>");
                    }
                    WriteLine();
                }
            }
        }
        //Added a helper RemoveRecordUsingEntityState(int carId) to delete records in DB by marking EntityState on an object which I want to delete, is found by carId.
        private static void RemoveRecordUsingEntityState(int carId)
        {
            using (var context = new AutoLotEntities())
            {
                //Create a new Car object and assign a carId to CarId, which I want to delete in DB.
                Car carToDelete = new Car()
                {
                    CarId = carId
                };

                //Set EntityState of carToDelete object to Deleted.
                //So far, I didn't trip to the DB, meaning good performance.
                context.Entry(carToDelete).State = EntityState.Deleted;
                try
                {
                    context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    WriteLine(ex);
                }
            }
        }
예제 #26
0
        private static void RemoveRecordUsingEntityState()
        {
            ForegroundColor = ConsoleColor.DarkCyan;
            WriteLine($"=> Deleting Record Using Entity State");

            int carId = AddNewRecord();

            ForegroundColor = ConsoleColor.DarkCyan;
            WriteLine($"CarID {carId} to delete");

            using (var context = new AutoLotEntities()) {
                Car carToDelete = new Car()
                {
                    CarId = carId
                };
                context.Entry(carToDelete).State = EntityState.Deleted;
                try {
                    context.SaveChanges();
                } catch (DbUpdateConcurrencyException ex) {
                    WriteLine(ex.Message);
                }
            }
        }
예제 #27
0
        private static void PrintAllInventory()
        {
            // Select all items from the Inventory table of AutoLot,
            // and print out the data using our custom ToString()
            // of the Inventory entity class.
            using (var context = new AutoLotEntities())
            {
                WriteLine("*** PrintAllInventory ***");
                WriteLine("Using context");
                foreach (Inventory c in context.Inventories)
                {
                    WriteLine(c);
                }

                WriteLine("Using a sql command");

                foreach (Inventory c in context.Inventories.SqlQuery("Select CarId,Make,Color,Name from Inventory where Make=@p0", "BMW"))
                {
                    WriteLine(c);
                }

                WriteLine("Using LINQ filter Only BMWs");

                foreach (Inventory c in context.Inventories.Where(c => c.Make == "BMW"))
                {
                    WriteLine(c);
                }

                WriteLine("Using orders in inventories - nested object from EF");

                foreach (Inventory c in context.Inventories)
                {
                    foreach (Order o in c.Orders)
                    {
                        WriteLine(o);
                    }
                }

                WriteLine("Using any orders for those in inventory using LINQ include");

                foreach (Inventory c in context.Inventories.Include(c => c.Orders))
                {
                    foreach (Order o in c.Orders)
                    {
                        WriteLine(o);
                    }
                }

                WriteLine("Using no lazy loading and direct using Entry()");

                context.Configuration.LazyLoadingEnabled = false;
                foreach (Inventory c in context.Inventories)
                {
                    context.Entry(c).Collection(x => x.Orders).Load();
                    foreach (Order o in c.Orders)
                    {
                        WriteLine(o);
                    }
                }
            }
        }
예제 #28
0
 public int Save(T entity)
 {
     _db.Entry(entity).State = EntityState.Modified;
     return(SaveChanges());
 }