static void Listing20_5() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // create the new customer Customer cust = new Customer { CustomerID = "LAWN", CompanyName = "Lawn Wranglers", ContactName = "Mr. Abe Henry", ContactTitle = "Owner", Address = "1017 Maple Leaf Way", City = "Ft. Worth", Region = "TX", PostalCode = "76104", Country = "USA", Phone = "(800) MOW-LAWN", Fax = "(800) MOW-LAWO" }; // create the new order Order ord = new Order { CustomerID = "LAWN", EmployeeID = 4, OrderDate = DateTime.Now, RequiredDate = DateTime.Now.AddDays(7), ShipVia = 3, Freight = new Decimal(24.66), ShipName = "Lawn Wranglers", ShipAddress = "1017 Maple Leaf Way", ShipCity = "Ft. Worth", ShipRegion = "TX", ShipPostalCode = "76104", ShipCountry = "USA" }; // attach the customer to the order ord.Customer = cust; // add the new Order to the context context.Orders.AddObject(ord); // save the changes context.SaveChanges(); // query to make sure the record is there Customer customer = context.Customers.Where(c => c.CustomerID == "LAWN").First(); Console.WriteLine("{0} - {1}", customer.CompanyName, customer.ContactName); Console.WriteLine("Customer has {0} orders", customer.Orders.Count()); // This part of the code resets the database context.DeleteObject(ord); context.DeleteObject(cust); context.SaveChanges(); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void Listing20_23() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // query for the first order for LAZYK Order firstOrder = context.Orders .Where(o => o.OrderID == 10248) .Select(o => o) .First(); // delete the Order_Detail objects for the order foreach (Order_Detail od in firstOrder.Order_Details.ToArray()) { Console.WriteLine("Deleting order detail {0}, {1}, {2}, {3}", od.OrderID, od.ProductID, od.UnitPrice, od.Quantity); context.DeleteObject(od); } // delete the order context.DeleteObject(firstOrder); // save the changes context.SaveChanges(); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
private void DeleteCustomers(List <Customer> customers) { foreach (Customer customer in customers) { entities.DeleteObject(customer); } }
public virtual void Delete(int id) { var entity = this.GetById(id); _context.DeleteObject(entity); _context.SaveChanges(); }
public void DeleteAndInsert(DateTime runDate, Employee employee) { using (var context = new NorthwindEntities()) using (var scope = new TransactionScope()) { EmployeeBonus bonusToDelete = (from bonus in context.EmployeeBonus where bonus.Date == runDate && bonus.EmployeeName == employee.Name select bonus).FirstOrDefault(); if (bonusToDelete != null) { context.DeleteObject(bonusToDelete); } var newBonus = new EmployeeBonus { EmployeeName = employee.Name, Date = runDate, Bonus = employee.Bonus }; context.EmployeeBonus.AddObject(newBonus); context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); scope.Complete(); } }
public void DeleteAndInsert(DateTime runDate, FreightByShipper freightByShipper) { using (var context = new NorthwindEntities()) using (var scope = new TransactionScope()) { var summaryToDelete = (from freightSummary in context.FreightSummaries where freightSummary.RunDate == runDate && freightSummary.ShipperName == freightByShipper.ShipperName select freightSummary).FirstOrDefault(); if (summaryToDelete != null) { context.DeleteObject(summaryToDelete); } var newFreightSummary = new FreightSummary() { Freight = freightByShipper.Freight, RunDate = runDate, ShipperName = freightByShipper.ShipperName }; context.FreightSummaries.AddObject(newFreightSummary); context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); scope.Complete(); } }
public void DeleteEmployee(Employee e) { NorthwindEntities db = new NorthwindEntities(); var data = from item in db.Employees where item.EmployeeID == e.EmployeeID select item; Employee obj = data.SingleOrDefault(); db.DeleteObject(obj); db.SaveChanges(); }
public void RevertEntityStateWithDeletedStateSucceeds() { // Arrange var svc = new NorthwindEntities(_serviceRoot); var entity = svc.Categories.First(); svc.DeleteObject(entity); // Act svc.RevertEntityState(entity); // Assert Assert.IsFalse(svc.HasPendingChanges()); }
private void DeleteSelectedCustomer(int rowHandle) { if (rowHandle < 0) { return; } if (MessageBox.Show("Do you really want to delete the selected customer?", "Delete Customer", MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } FindCustomerByIDAndProcess(GetCustomerIDByRowHandle(rowHandle), customer => { context.DeleteObject(customer); SaveChandes(); }); }
static void Listing20_1() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // step 1. Create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // Step 2. Create a new customer object Customer cust = new Customer() { CustomerID = "LAWN", CompanyName = "Lawn Wranglers", ContactName = "Mr. Abe Henry", ContactTitle = "Owner", Address = "1017 Maple Leaf Way", City = "Ft. Worth", Region = "TX", PostalCode = "76104", Country = "USA", Phone = "(800) MOW-LAWN", Fax = "(800) MOW-LAWO" }; // Step 3. Add to the ObjectSet<Customer> context.Customers.AddObject(cust); // Step 4. Save the changes context.SaveChanges(); // Query the record. Customer customer = context.Customers.Where(c => c.CustomerID == "LAWN").First(); Console.WriteLine("{0} - {1}", customer.CompanyName, customer.ContactName); // Reset the database so the example can be run more than once. Console.WriteLine("Deleting the added customer LAWN."); context.DeleteObject(customer); context.SaveChanges(); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void Listing20_24() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // query for the order Order firstOrder = context.Orders .Where(o => o.OrderID == 10248) .Select(o => o) .First(); // delete the order context.DeleteObject(firstOrder); // save the changes context.SaveChanges(); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void Listing21_11() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // get the order details for order 10248 IQueryable <Order_Detail> ods = (from o in context.Order_Details where o.OrderID == 10248 select o); foreach (Order_Detail od in ods) { context.DeleteObject(od); } // save the changes context.SaveChanges(); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void Listing20_2() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // create a new customer object Customer cust = Customer.CreateCustomer("LAWN", "Lawn Wranglers"); // populate the nullable fields cust.ContactName = "Mr. Abe Henry"; cust.ContactTitle = "Owner"; cust.Address = "1017 Maple Leaf Way"; cust.City = "Ft. Worth"; cust.Region = "TX"; cust.PostalCode = "76104"; cust.Country = "USA"; cust.Phone = "(800) MOW-LAWN"; cust.Fax = "(800) MOW-LAWO"; // add the new customer to the Customers ObjectSet context.Customers.AddObject(cust); // save the changes context.SaveChanges(); // Query the record. Customer customer = context.Customers.Where(c => c.CustomerID == "LAWN").First(); Console.WriteLine("{0} - {1}", customer.CompanyName, customer.ContactName); // Reset the database so the example can be run more than once. Console.WriteLine("Deleting the added customer LAWN."); context.DeleteObject(customer); context.SaveChanges(); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void Main(string[] args) { // create a context NorthwindEntities context = new NorthwindEntities(); // query to find the empoyee record we are looking for IEnumerable <Employee> results = from e in context.Employees where e.LastName == "Freeman" select e; // enumerate the results and delete them foreach (Employee emp in results) { context.DeleteObject(emp); } // save the changes context.SaveChanges(); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
static void Listing20_20() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); // create the ObjectContext NorthwindEntities context = new NorthwindEntities(); // get the order details for order 10248 IQueryable <Order_Detail> ods = from o in context.Order_Details where o.OrderID == 10248 select o; // print out the query results Console.WriteLine("Before deletion"); foreach (Order_Detail od in ods) { Console.WriteLine("Order detail {0}, {1}, {2}", od.ProductID, od.UnitPrice, od.Quantity); } // delete the first order detail context.DeleteObject(ods.First()); // save the changes context.SaveChanges(); // print out the query results Console.WriteLine("After deletion"); foreach (Order_Detail od in ods) { Console.WriteLine("Order detail {0}, {1}, {2}", od.ProductID, od.UnitPrice, od.Quantity); } Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }