private static void CallStoredProc() { using (AutoLotEntities context = new AutoLotEntities()) { ObjectParameter outParam = new ObjectParameter("petName", typeof(string)); context.GetPetName(83, outParam); Console.WriteLine(outParam.Value); } }
private static void AddNewCars(IEnumerable <Car> carsToAdd) { using (var context = new AutoLotEntities()) { context.Cars.AddRange(carsToAdd); context.SaveChanges(); } }
private static void PrintCars(string make) { using (var context = new AutoLotEntities()) { Console.WriteLine($"-------------- Cars With Make={make} ------------------"); foreach (var item in context.Cars.Where(c => c.Make == make)) { Console.WriteLine(item); } } }
private static void PrintCar(int key) { Console.WriteLine($"-------------- Cars With Key = {key} ------------------"); using (var context = new AutoLotEntities()) { Console.WriteLine(context.Cars.Find(key)); } Console.WriteLine($"------------------------------------------"); }
private static void PrintAllInventory() { Console.WriteLine($"-------------- All Cars ------------------"); using (var context = new AutoLotEntities()) { foreach (var item in context.Cars) { Console.WriteLine(item); } } Console.WriteLine($"------------------------------------------"); }
private static void PrintAllInventoryNonTrackableCode() { using (var context = new AutoLotEntities()) { string sql = @"SELECT CarId,Make,Color,PetName as CarNickName FROM Inventory"; Console.WriteLine($"-------------- All Cars in Inventory ------------------"); foreach (var item in context.Cars.SqlQuery(sql)) { Console.WriteLine(item); } } }
private static void PrintCurtomerOrders(string custID) { int id = int.Parse(custID); using (AutoLotEntities context = new AutoLotEntities()) { var carsOnOrder = from o in context.Orders where o.CustID == id select o.Inventory; Console.WriteLine(string.Format("Customer has {0} orders pending", carsOnOrder.Count())); foreach (var item in carsOnOrder) { Console.WriteLine("-> {0} {1} named {2}.", item.Color, item.Make, item.PetName); } } }
private static void PrintCars(params int[] keys) { Console.WriteLine($"-------------- Cars With Keys {string.Join(", ", keys)} ------------------"); using (var context = new AutoLotEntities()) { foreach (var item in context.Cars.Where(c => keys.Contains(c.CarId))) { Console.WriteLine(item); } } Console.WriteLine($"------------------------------------------"); }
// Chaining Linq Queries private static void ChainingLinqQueries() { using (var context = new AutoLotEntities()) { //Not executed DbSet <Car> allData = context.Cars; //Not Executed. var colorsMakes = from item in allData select new { item.Color, item.Make }; //Now it's executed foreach (var item in colorsMakes) { Console.WriteLine(item); } } }
private static void PrintWithLINQ2() { using (var context = new AutoLotEntities()) { Console.WriteLine($"-------------- Cars Using LINQ ------------------"); // Get a projection of new data. var shortCars = from car in context.Cars select new { car.CarNickName, car.Color }; foreach (var item in shortCars) { Console.WriteLine($"Anonymous Type: Name={item.CarNickName},\t Color={item.Color}"); } } }
private static void PrintCarsWithMakeNonTrackableCode(string make) { using (var context = new AutoLotEntities()) { string sql = @"SELECT CarId, Make, Color, PetName AS CarNickName FROM Inventory WHERE Make= @P0 "; Console.WriteLine($"-------------- Cars With Make={make} ------------------"); foreach (var item in context.Cars.SqlQuery(sql, make)) { Console.WriteLine(item); } } }
private static void PrintWithLINQ() { using (var context = new AutoLotEntities()) { Console.WriteLine($"-------------- Cars Using LINQ ------------------"); var query = from car in context.Cars where car.CarId == 1 || car.CarId == 2 select car; foreach (var item in query) { Console.WriteLine(item); } } }
private static void CallStoredProc() { using (AutoLotEntities context = new AutoLotEntities()) { ObjectParameter input = new ObjectParameter("carID", 83); ObjectParameter output = new ObjectParameter("petName", typeof(string)); // Call ExecuteFunction off the context.... context.ExecuteFunction("GetPetName", input, output); // ....or use the strongly typed method on the context. context.GetPetName(83, output); Console.WriteLine("Car #83 is named {0}", output.Value); } }
private static int AddNewCar(Car car) { using (AutoLotEntities context = new AutoLotEntities()) { try { context.Cars.Add(car); context.SaveChanges(); // On a successful save, EF populates the database generated identity field. return(car.CarId); } catch (Exception ex) { Console.WriteLine(ex.InnerException?.Message); return(0); } } }
private void ConfigureGrid() { using (AutoLotEntities context = new AutoLotEntities()) { // Build a LINQ query that gets back some data from the Inventory table. var dataToShow = from c in context.Inventories select new { c.CarID, c.Make, c.Color, c.PetName }; this.gridInventory.ItemsSource = dataToShow; } }