// // Reminder: Ensure LazyLoading is disabled // public static void ShowExplicitLoading() { Console.WriteLine("Load related entities using Explicit Loading:\n"); using (var context = new AdventureWorksEntities()) { // Load a Product var product = context.Products.Find(942); // Explicitly load the work orders context.Entry(product).Collection(x => x.WorkOrders).Load(); var worCount = 0; foreach (var workOrder in product.WorkOrders) { // Explicitly load the work order routings context.Entry(workOrder).Collection(x => x.WorkOrderRoutings).Load(); worCount += workOrder.WorkOrderRoutings.Count; } Console.WriteLine("Product Id: {0}", product.ProductID); Console.WriteLine("Product Name: {0}", product.Name); Console.WriteLine("Work Orders: {0}", product.WorkOrders.Count); Console.WriteLine("Work Order Routings: {0}", worCount); } }
/* * Use ObjectQuery(T) Class or ObjectSet<T> on the object context to access data using Entity SQL Builder methods. * Both methods requires an instance of type ObjectContext. As a result, Entity SQL Builder methods are not supported * on objects of type DbContext. */ public static void RunESQLBuilderExample() { using (var context = new AdventureWorksEntities()) { System.Console.WriteLine("\nUsing Entity SQL Builder"); if (context.GetType() == typeof (ObjectContext)) { // An example of ESQL Builder using an ObjectSet<T> on the ObjectContext // var order = context.CreateObjectSet<SalesOrderHeader>("SalesOrderHeader").Where("it.SalesOrderID = 43661"); // System.Console.WriteLine("\nSalesOrderID: {0} \nOrderDate: {1} \nDueDate: {2} \nShipDate: {3}", order.SalesOrderID, order.OrderDate, order.DueDate, order.ShipDate); var objContext = new ObjectContext("name=AdventureWorksEntities"); var queryString = @"SELECT VALUE salesOrders FROM AdventureWorksEntities.SalesOrderHeader AS salesOrders"; var salesQuery1 = new ObjectQuery<SalesOrderHeader>(queryString, objContext, MergeOption.NoTracking); var salesQuery2 = salesQuery1.Where("it.SalesOrderID = 43661"); foreach (var order in salesQuery2) { System.Console.WriteLine("\nSalesOrderID: {0} \nOrderDate: {1} \nDueDate: {2} \nShipDate: {3}", order.SalesOrderID, order.OrderDate, order.DueDate, order.ShipDate); } } else { System.Console.WriteLine("\nSorry! Context is not of type ObjectContext. ESQL Builder is not supported."); } } }
public static void FindEntityUsingPk() { System.Console.WriteLine("\nFinding Identity with PK"); using (var context = new AdventureWorksEntities()) { var order = context.SalesOrderHeaders.Find(43661); System.Console.WriteLine("\nSalesOrderID: {0} \nOrderDate: {1} \nDueDate: {2} \nShipDate: {3}", order.SalesOrderID, order.OrderDate, order.DueDate, order.ShipDate); } }
public static void GetEmployeeManagers() { Console.WriteLine("Employee Managers using Stored Procedures:\n"); using (var context = new AdventureWorksEntities()) { var entityId = 6; var managerResults = context.uspGetEmployeeManagers(entityId).ToList(); var firstEmployee = managerResults.ElementAt(0); Console.WriteLine("Managers for {0} {1}\n", firstEmployee.FirstName, firstEmployee.LastName); foreach (var result in managerResults) { Console.WriteLine("Manager Name: {0} {1}", result.ManagerFirstName, result.ManagerLastName); } } }
public static void ShowEagerLoading() { Console.WriteLine("Load related entities using Eager Loading:\n"); using (var context = new AdventureWorksEntities()) { var product = context.Products.Include(x => x.ProductInventories).FirstOrDefault(x => x.ProductID == 1); Console.WriteLine("Product: {0}\n", product.Name); Console.WriteLine("{0, 10} {1, 10} {2, 10} {3, 10} {4, 10}", "ProductID", "LocationID", "Shelf", "Bin", "Quantity"); Console.WriteLine("--------------------------------------------------------"); foreach (var item in product.ProductInventories) { Console.WriteLine("{0, 10} {1, 10} {2, 10} {3, 10} {4, 10}", item.ProductID, item.LocationID, item.Shelf, item.Bin, item.Quantity); } } }
public static void ShowProducts() { Console.WriteLine("Adventure Works Products:\n"); using (var ctx = new AdventureWorksEntities()) { var products = ctx.Products.Take(4); foreach (var product in products) { Console.WriteLine("ID: {0}", product.ProductID); Console.WriteLine("Name: {0}", product.Name); Console.WriteLine("Number: {0}", product.ProductNumber); Console.WriteLine("Price: {0}", product.ListPrice); Console.WriteLine("----------------------------------"); } } Console.ReadKey(); }
public static void GetDaysfromOrders() { System.Console.WriteLine("Number of Days from Order - Function Example:\n"); using (var context = new AdventureWorksEntities()) { var days = (from o in context.SalesOrderHeaders where EntityFunctions.DiffDays(o.OrderDate, o.ShipDate) > 3 select o).Take(4); foreach (var day in days) { System.Console.WriteLine("OrderNumber: 000{0}", day.SalesOrderID); System.Console.WriteLine("Took {0} Days", (day.ShipDate - day.OrderDate).Value.Days); System.Console.WriteLine("Was Ordered on {0} and Shipped on {1}", day.OrderDate.Date.ToShortDateString(), day.ShipDate.Value.Date.ToShortDateString()); System.Console.WriteLine("==============================================="); } } }
public static void ShowEagerLoadingMultipleLevels() { Console.WriteLine("Load related entities using Eager Loading at multiple levels:\n"); using (var context = new AdventureWorksEntities()) { // Load all Products, all related Work Orders, and all related Work Order Routings var products = context.Products.Include(x => x.WorkOrders.Select(w => w.WorkOrderRoutings)).ToList(); foreach (var product in products) { // Select Work Order Routings Count for each Work Order // Work Order Routing count is obtained from the products object graph in the conceptual model var worCount = product.WorkOrders.SelectMany(o => o.WorkOrderRoutings).Count(); Console.WriteLine("Product Id: {0}", product.ProductID); Console.WriteLine("Product Name: {0}", product.Name); Console.WriteLine("Work Orders: {0}", product.WorkOrders.Count); Console.WriteLine("Work Order Routings: {0}", worCount); Console.WriteLine("--------------------------------"); } } }
public static void RunLinqExample1() { System.Console.WriteLine("\nUsing LINQ"); using (var context = new AdventureWorksEntities()) { var order = (from o in context.SalesOrderHeaders where o.SalesOrderID == 43661 select o).FirstOrDefault(); System.Console.WriteLine("\nSalesOrderID: {0} \nOrderDate: {1} \nDueDate: {2} \nShipDate: {3}", order.SalesOrderID, order.OrderDate, order.DueDate, order.ShipDate); } }
public static void RunRawSQL() { System.Console.WriteLine("\nUsing Raw SQL"); using (var context = new AdventureWorksEntities()) { var order = context.SalesOrderHeaders.SqlQuery("SELECT * FROM Sales.SalesOrderHeader").FirstOrDefault(x => x.SalesOrderID == 43661); System.Console.WriteLine("\nSalesOrderID: {0} \nOrderDate: {1} \nDueDate: {2} \nShipDate: {3}", order.SalesOrderID, order.OrderDate, order.DueDate, order.ShipDate); } }
public static void RunLinqExample2() { System.Console.WriteLine("\nUsing LINQ Expression"); using (var context = new AdventureWorksEntities()) { var order = context.SalesOrderHeaders.FirstOrDefault(x => x.SalesOrderID == 43661); System.Console.WriteLine("\nSalesOrderID: {0} \nOrderDate: {1} \nDueDate: {2} \nShipDate: {3}", order.SalesOrderID, order.OrderDate, order.DueDate, order.ShipDate); } }
public static void ShowLazyLoading() { Console.WriteLine("Load related entities using Lazy Loading at multiple levels:\n"); using (var context = new AdventureWorksEntities()) { if (!context.Configuration.LazyLoadingEnabled) { Console.WriteLine("LazyLoading is disabled...related entities will not be loaded!"); Thread.Sleep(5000); } // Load all Products var products = context.Products.ToList(); foreach (var product in products) { // Select Work Order Routings Count for each Work Order // Work Order Routing count is obtained from the storage model; results in multiple trips to Db var worCount = product.WorkOrders.SelectMany(o => o.WorkOrderRoutings).Count(); Console.WriteLine("Product Id: {0}", product.ProductID); Console.WriteLine("Product Name: {0}", product.Name); Console.WriteLine("Work Orders: {0}", product.WorkOrders.Count); Console.WriteLine("Work Order Routings: {0}", worCount); Console.WriteLine("--------------------------------"); } } }