static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from Chapter10.Customer"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var c1 = new Category { CategoryName = "Backpacking Tents" }; new Product { ProductName = "Hooligan", UnitPrice = 89.99M, Category = c1 }; new Product { ProductName = "Kraz", UnitPrice = 99.99M, Category = c1 }; new Product { ProductName = "Sundome", UnitPrice = 49.99M, Category = c1 }; context.Categories.AddObject(c1); var c2 = new Category { CategoryName = "Family Tents" }; new Product { ProductName = "Evanston", UnitPrice = 169.99M, Category = c2 }; new Product { ProductName = "Montana", UnitPrice = 149.99M, Category = c2 }; context.Categories.AddObject(c2); context.SaveChanges(); } // with esql using (var context = new EFRecipesEntities()) { Console.WriteLine("Using esql for the query..."); Console.WriteLine(); string sql = @"Select c.CategoryName, EFRecipesModel.AverageUnitPrice(c) as AveragePrice from EFRecipesEntities.Categories as c"; var cats = context.CreateQuery <DbDataRecord>(sql); foreach (var cat in cats) { Console.WriteLine("Category '{0}' has an average price of {1}", cat[0], ((decimal)cat[1]).ToString("C")); } } // with LINQ using (var context = new EFRecipesEntities()) { Console.WriteLine(); Console.WriteLine("Using LINQ for the query..."); Console.WriteLine(); var cats = from c in context.Categories select new { Name = c.CategoryName, AveragePrice = MyFunctions.AverageUnitPrice(c) }; foreach (var cat in cats) { Console.WriteLine("Category '{0}' has an average price of {1}", cat.Name, cat.AveragePrice.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter12.applicant"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var path1 = "AlexJones.txt"; File.AppendAllText(path1, "Alex Jones\nResume\n..."); var path2 = "JanisRogers.txt"; File.AppendAllText(path2, "Janis Rodgers\nResume\n..."); var app1 = new Applicant { Name = "Alex Jones", ResumePath = path1 }; var app2 = new Applicant { Name = "Janis Rogers", ResumePath = path2 }; context.Applicants.AddObject(app1); context.Applicants.AddObject(app2); context.SaveChanges(); // delete Alex Jones context.Applicants.DeleteObject(app1); context.SaveChanges(); } Console.WriteLine("Press <enter> to continue...."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var customer = new Customer { Name = "Phil Marlowe", Email = "*****@*****.**", Phone = "876-5309" }; var order = new Order { DateOrdered = DateTime.Parse("4/18/2010"), InvoiceNumber = 12224, Customer = customer }; context.Customers.AddObject(customer); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var customer in context.Customers) { Console.WriteLine("Customer: {0}, email: {1}", customer.Name, customer.Email); foreach (var order in customer.Orders) { Console.WriteLine("\t{0} {1}", order.OrderId, order.DateOrdered); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
private static void RunExample() { using (var context = new EFRecipesEntities()) { context.Employees.Add(new SalariedEmployee { Name = "Robin Rosen", Salary = 89900M }); context.Employees.Add(new HourlyEmployee { Name = "Steven Fuller", Rate = 11.50M }); context.Employees.Add(new HourlyEmployee { Name = "Karen Steele", Rate = 12.95m }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { // a typical way to get Steven Fuller's entity var emp1 = context.Employees.Single(e => e.Name == "Steven Fuller"); Console.WriteLine("{0}'s rate is: {1} per hour", emp1.Name, ((HourlyEmployee)emp1).Rate.ToString("C")); // slightly more efficient way if we know that Steven is an HourlyEmployee var emp2 = context.Employees.OfType <HourlyEmployee>().Single(e => e.Name == "Steven Fuller"); Console.WriteLine("{0}'s rate is: {1} per hour", emp2.Name, emp2.Rate.ToString("C")); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
public void DeletePayment(Payment payment) { using (var context = new EFRecipesEntities()) { context.Entry(payment).State = EntityState.Deleted; context.SaveChanges(); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter11.product"); context.ExecuteStoreCommand("delete from chapter11.category"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter15.contact"); context.ExecuteStoreCommand("delete from chapter15.account"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter6.EventOrganizer"); context.ExecuteStoreCommand("delete from chapter6.Event"); context.ExecuteStoreCommand("delete from chapter6.Organizer"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter5.customeremail"); context.ExecuteStoreCommand("delete from chapter5.customer"); context.ExecuteStoreCommand("delete from chapter5.customertype"); } }
private static void Cleanup() { using (var context = new EFRecipesEntities()) { context.Database.ExecuteSqlCommand("delete from chapter13.hourlyemployee"); context.Database.ExecuteSqlCommand("delete from chapter13.salariedemployee"); context.Database.ExecuteSqlCommand("delete from chapter13.employee"); } }
public void DeletePayment(Payment payment) { using (var context = new EFRecipesEntities()) { context.Payments.Attach(payment); context.Payments.DeleteObject(payment); context.SaveChanges(); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter8.orderdetail"); context.ExecuteStoreCommand("delete from chapter8.[order]"); context.ExecuteStoreCommand("delete from chapter8.product"); context.ExecuteStoreCommand("delete from chapter8.customer"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var tea = new Product { ProductName = "Green Tea", UnitPrice = 1.09M }; var coffee = new Product { ProductName = "Colombian Coffee", UnitPrice = 2.15M }; var customer = new Customer { ContactName = "Karen Marlowe" }; var order1 = new Order { OrderDate = DateTime.Parse("4/19/10") }; order1.OrderDetails.Add(new OrderDetail { Product = tea, Quantity = 4, UnitPrice = 1.00M }); order1.OrderDetails.Add(new OrderDetail { Product = coffee, Quantity = 3, UnitPrice = 2.15M }); customer.Orders.Add(order1); context.Customers.AddObject(customer); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var query = context.Customers.Include("Orders.OrderDetails.Product"); foreach (var customer in query) { Console.WriteLine("Orders for {0}", customer.ContactName); foreach (var order in customer.Orders) { Console.WriteLine("--Order Date: {0}--", order.OrderDate.ToShortDateString()); foreach (var detail in order.OrderDetails) { Console.WriteLine("\t{0}, {1} units at {2} each, unit discount: {3}", detail.Product.ProductName, detail.Quantity.ToString(), detail.UnitPrice.ToString("C"), (detail.Product.UnitPrice - detail.UnitPrice).ToString("C")); } } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var product1 = new Product { SKU = "CAMP-136", ShortDesription = "High country camping tent", Description = "Use this tent on your next high country adventure.", UnitPrice = 199.95M }; context.Products.AddObject(product1); context.SaveChanges(); Console.WriteLine("Inserted Product {0}: {1}", product1.SKU, product1.ShortDesription); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var org = new Organizer { Name = "Community Charity" }; var evt = new Event { Name = "Fundraiser" }; org.Events.Add(evt); context.Organizers.AddObject(org); org = new Organizer { Name = "Boy Scouts" }; evt = new Event { Name = "Eagle Scout Dinner" }; org.Events.Add(evt); context.Organizers.AddObject(org); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var evsorg1 = from ev in context.Events from organizer in ev.Organizers select new { ev.EventId, organizer.OrganizerId }; Console.WriteLine("Using nested from clauses..."); foreach (var pair in evsorg1) { Console.WriteLine("EventId {0}, OrganizerId {1}", pair.EventId.ToString(), pair.OrganizerId.ToString()); } var evsorg2 = context.Events.SelectMany(e => e.Organizers, (ev, org) => new { ev.EventId, org.OrganizerId }); Console.WriteLine("\nUsing SelectManay()"); foreach (var pair in evsorg2) { Console.WriteLine("EventId {0}, OrganizerId {1}", pair.EventId.ToString(), pair.OrganizerId.ToString()); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
public Payment InsertPayment() { using (var context = new EFRecipesEntities()) { // delete previous text data context.ExecuteStoreCommand("delete from chapter9.payment"); context.ExecuteStoreCommand("delete from chapter9.invoice"); var payment = new Payment { Amount = 99.95M, Invoice = new Invoice { Description = "Auto Repair" } }; context.Payments.AddObject(payment); context.SaveChanges(); return(payment); } }
static void RunExample() { // insert a couple rows using (var context = new EFRecipesEntities()) { string sql = @"insert into Chapter3.Payment(Amount, Vendor) values (@Amount, @Vendor)"; var args = new DbParameter[] { new SqlParameter { ParameterName = "Amount", Value = 99.97M }, new SqlParameter { ParameterName = "Vendor", Value = "Ace Plumbing" } }; int rowCount = context.ExecuteStoreCommand(sql, args); args = new DbParameter[] { new SqlParameter { ParameterName = "Amount", Value = 43.83M }, new SqlParameter { ParameterName = "Vendor", Value = "Joe's Trash Service" } }; rowCount += context.ExecuteStoreCommand(sql, args); Console.WriteLine("{0} rows inserted", rowCount.ToString()); } // materialize some entities using (var context = new EFRecipesEntities()) { Console.WriteLine("Payments"); Console.WriteLine("========"); foreach (var payment in context.Payments) { Console.WriteLine("Paid {0} to {1}", payment.Amount.ToString("C"), payment.Vendor); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
protected void Page_Load(object sender, EventArgs e) { using (var context = new EFRecipesEntities()) { // delete any previous data we might have context.ExecuteStoreCommand("delete from chapter4.customer"); // insert some data context.Customers.AddObject(new Customer { Name = "Robin Rosen", City = "Olathe", State = "KS" }); context.Customers.AddObject(new Customer { Name = "John Wise", City = "Springtown", State = "TX" }); context.Customers.AddObject(new Customer { Name = "Karen Carter", City = "Raytown", State = "MO" }); context.SaveChanges(); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.Products.AddObject(new Product { Name = "High Country Backpacking Tent", UnitPrice = 199.95M }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { // get the product var product = context.Products.SingleOrDefault(); Console.WriteLine("{0} Unit Price: {1}", product.Name, product.UnitPrice.ToString("C")); // delete out of band context.ExecuteStoreCommand(@"update chapter14.product set unitprice = 229.95 where productId = @p0", product.ProductId); // update the product the via the model product.UnitPrice = 239.95M; Console.WriteLine("Changing {0}'s Unit Price to: {1}", product.Name, product.UnitPrice.ToString("C")); try { context.SaveChanges(); } catch (OptimisticConcurrencyException ex) { Console.WriteLine("Concurrency Exception! {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("Exception! {0}", ex.Message); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var person = new Person() { FirstName = "Robert", MiddleName = "Allen", LastName = "Doe", PhoneNumber = "867-5309" }; context.People.AddObject(person); person = new Person() { FirstName = "John", MiddleName = "K.", LastName = "Smith", PhoneNumber = "824-3031" }; context.People.AddObject(person); person = new Person() { FirstName = "Billy", MiddleName = "Albert", LastName = "Minor", PhoneNumber = "907-2212" }; context.People.AddObject(person); person = new Person() { FirstName = "Kathy", MiddleName = "Anne", LastName = "Ryan", PhoneNumber = "722-0038" }; context.People.AddObject(person); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var person in context.People) { System.Console.WriteLine("{0} {1} {2}, Phone: {3}", person.FirstName, person.MiddleName, person.LastName, person.PhoneNumber); } } System.Console.WriteLine("Press <enter> to continue..."); System.Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var acc1 = new Account { Balance = 99.34M }; var con1 = new Contact { Name = "Stacy Jones", Phone = "867-5301" }; var cus1 = new Customer { Name = "Bill Waters", Phone = "907-2212", Account = acc1 }; context.Contacts.AddObject(con1); context.Contacts.AddObject(cus1); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = true; Console.WriteLine("All Contacts"); Console.WriteLine("============"); foreach (var contact in context.Contacts) { Console.WriteLine("{0} {1}", contact.Name, contact.Phone); } Console.WriteLine("Just Customers"); foreach (var contact in context.Contacts.OfType <Customer>()) { Console.WriteLine("\t{0} {1} (Balance: {2})", contact.Name, contact.Phone, contact.Account.Balance.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var c1 = new Customer { Name = "Robin Steele", Company = "GoShopNow.com", ContactTitle = "CEO" }; var c2 = new Customer { Name = "Orin Torrey", Company = "GoShopNow.com", ContactTitle = "Sales Manager" }; var c3 = new Customer { Name = "Robert Lancaster", Company = "GoShopNow.com", ContactTitle = "Sales Manager" }; var c4 = new Customer { Name = "Julie Stevens", Company = "GoShopNow.com", ContactTitle = "Sales Manager" }; context.Customers.AddObject(c1); context.Customers.AddObject(c2); context.Customers.AddObject(c3); context.Customers.AddObject(c4); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var allCustomers = context.GetCustomers("GoShopNow.com", "Sales Manager"); Console.WriteLine("Customers that are Sales Managers at GoShopNow.com"); foreach (var c in allCustomers) { Console.WriteLine("Customer: {0}", c.Name); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var web = new CustomerType { Description = "Web Customer", CustomerTypeId = 1 }; var retail = new CustomerType { Description = "Retail Customer", CustomerTypeId = 2 }; var customer = new Customer { Name = "Joan Smith", CustomerType = web }; customer.CustomerEmails.Add(new CustomerEmail { Email = "*****@*****.**" }); customer.CustomerEmails.Add(new CustomerEmail { Email = "*****@*****.**" }); context.Customers.AddObject(customer); customer = new Customer { Name = "Bill Meyers", CustomerType = retail }; customer.CustomerEmails.Add(new CustomerEmail { Email = "*****@*****.**" }); context.Customers.AddObject(customer); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var customers = context.Customers.Include("CustomerType").Include("CustomerEmails"); Console.WriteLine("Customers"); Console.WriteLine("========="); foreach (var customer in customers) { Console.WriteLine("{0} is a {1}, email address(es)", customer.Name, customer.CustomerType.Description); foreach (var email in customer.CustomerEmails) { Console.WriteLine("\t{0}", email.Email); } } } using (var context = new EFRecipesEntities()) { var customTypes = context.CustomerTypes.Include("Customers.CustomerEmails"); Console.WriteLine("\nCustomers by Type"); Console.WriteLine("================="); foreach (var customerType in customTypes) { Console.WriteLine("Customer type: {0}", customerType.Description); foreach (var customer in customerType.Customers) { Console.WriteLine("{0}", customer.Name); foreach (var email in customer.CustomerEmails) { Console.WriteLine("\t{0}", email.Email); } } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
private static void RunExample() { using (var context = new EFRecipesEntities()) { var web = new CustomerType { Description = "Web Customer", CustomerTypeId = 1 }; var retail = new CustomerType { Description = "Retail Customer", CustomerTypeId = 2 }; var customer = new Customer { Name = "Joan Smith", CustomerType = web }; customer.CustomerEmails.Add(new CustomerEmail { Email = "*****@*****.**" }); customer.CustomerEmails.Add(new CustomerEmail { Email = "*****@*****.**" }); context.Customers.Add(customer); customer = new Customer { Name = "Bill Meyers", CustomerType = retail }; customer.CustomerEmails.Add(new CustomerEmail { Email = "*****@*****.**" }); context.Customers.Add(customer); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var customers = context.Customers; Console.WriteLine("Customers"); Console.WriteLine("========="); // Only information from the Customer entity is requested foreach (var customer in customers) { Console.WriteLine("Customer name is {0}", customer.Name); } // Now, application is requesting information from the related entities, CustomerType // and CustomerEmail, resulting in Entity Framework generating separate queries to each // entity object in order to obtain the requested information. foreach (var customer in customers) { Console.WriteLine("{0} is a {1}, email address(es)", customer.Name, customer.CustomerType.Description); foreach (var email in customer.CustomerEmails) { Console.WriteLine("\t{0}", email.Email); } } // Extra credit: // If you enable SQL Server Profiler, the following query will not requery the database // for related data. Instead, it will return the in-memory data from the prior // query. foreach (var customer in customers) { Console.WriteLine("{0} is a {1}, email address(es)", customer.Name, customer.CustomerType.Description); foreach (var email in customer.CustomerEmails) { Console.WriteLine("\t{0}", email.Email); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }