static void Main(string[] args) { /* int count = 0; AdventureWorks2012Entities adventure = new AdventureWorks2012Entities(); foreach( Person person in adventure.People) { if (count++ > 20) { break; } Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.rowguid); } foreach (EmailAddress email in adventure.EmailAddresses) { Console.WriteLine(email); } Console.ReadKey(); */ CarsJWallerEntities1 carsTest = new CarsJWallerEntities1(); foreach (Car car in carsTest.Cars) { Console.WriteLine(car.id + " " + car.make + " " + car.model); } //Find by any property string userInput = string.Empty; Console.Write("Input "); userInput = Console.ReadLine(); Car carToFind = carsTest.Cars.Where(car => car.id == userInput).First(); // Car OtherCarToFind = carsTest.Cars.Where(car => car.make == "Challenger").First(); Console.WriteLine(carToFind.id + " " + carToFind.make + " " + carToFind.model); // Console.WriteLine(OtherCarToFind.id + " " + OtherCarToFind.make + " " + OtherCarToFind.model); //Find Car Based on Primary ID // Car foundCar = carsTest.Cars.Find("V0LCD1814"); // Console.WriteLine(foundCar.id + " " + foundCar.make + " " + foundCar.model); Console.ReadKey(); // ADD new Car Car newCar = new Car(); newCar.id = "00000"; newCar.make = "Nissan"; carsTest.Cars.Add(newCar); carsTest.SaveChanges(); Car carToFindForUpdate = carsTest.Cars.Find(userInput); }
static void Main(string[] args) { //AdventureWorks2012Entities adventure = new AdventureWorks2012Entities(); //int counter = 0; //Guid id = Guid.NewGuid(); //foreach (Person person in adventure.People) //{ // if (counter == 0) // { // id = person.rowguid; // } // if (counter++ > 20) // { // break; // } // Console.WriteLine(counter +" "+ person.FirstName + " " + person.LastName + " " + person.rowguid); //} //Console.WriteLine(); //Console.WriteLine(); //Console.WriteLine(adventure.People.Find(id)); //foreach (EmailAddress email in adventure.EmailAddresses) //{ // Console.WriteLine(email); //} //************************************************************** //List out all of the cars in the table //************************************************************** //Get access to the collection of tables we can interact with. CarsYWangEntities carsYWangEntities = new CarsYWangEntities(); //Loop through all of the cars in the table called Cars. foreach(Car car in carsYWangEntities.Cars) { Console.WriteLine("ID: "+car.id + " " + "MAKE: "+car.make + " " + "MODEL:"+car.model); } //************************************************************** //Find a specific one by any property //************************************************************** //FIRST one to find back //Call the Where method on the table Cars and pass it in a lambda expression for //the criteria we are looking for. There is nothing special about the work car //in the part that reads: car => car.id =="VOLCD1914" . It could be any characters //we want it to be. car made sense. Also, car represents the object we want to do //the where clause on. That's why we have car.id =="VOLCD1914". Car carToFind = carsYWangEntities.Cars.Where(c => c.id == "V0LCD1814").First(); //We can look for a specific model from the database with a where clause based on any criteria we want. //Here we are doing it with the Car's model instead of its id. Car otherCarToFind = carsYWangEntities.Cars.Where(car => car.model == "Challenger").First(); //Print them out. Console.WriteLine(); Console.WriteLine(); Console.WriteLine(carToFind.id + " " + carToFind.make + " " + carToFind.model ); Console.WriteLine(otherCarToFind.id + " " + otherCarToFind.make + " " + otherCarToFind.model); //********************************************************************************* //Find a car based on the primary id //********************************************************************************* //NOTE:This currently doesn't work because I forgot to set a primary id on the table. //Will hopefully fix it soon. //Pull out a car from the table based on the primary Id //TODO: uncomment when primary id is fixed // Car foundCar = carsYWangEntities.Cars.Find("V0LCD1814"); //Print it out //Console.WriteLine(); //Console.WriteLine(); //Console.WriteLine("Hohoho"); //Console.WriteLine(foundCar.id + " " + foundCar.make + " " + foundCar.model); //******************************************************************************* //Add a new Car to the database //******************************************************************************* //Make an instance of a new car Car newCarToAdd = new Car(); //Assign properties to the parts of the model newCarToAdd.id = "88888"; newCarToAdd.make = "Nissan"; newCarToAdd.model = "GT-R"; newCarToAdd.horsepower = 550; newCarToAdd.cylinders = 8; newCarToAdd.year = "2016"; newCarToAdd.type = "Car"; //Add the new car to the Cars table //TODO: UNCOMMENT WHEN PRIMARY ID IS FIXED // carsYWangEntities.Cars.Add(newCarToAdd); //This method call actually does the work of saving the changes to the database. //carsYWangEntities.SaveChanges(); //********************************************************************** //How to do an update //**************************************************************** //Get a car out of the database that we would like to update Car carToFindForUpdate = carsYWangEntities.Cars.Find("V0LCD1814"); //Update some of the properties of the car we found. Dont need to update all of them //if we don't want to. I choose these 4. carToFindForUpdate.make = "Nissan"; carToFindForUpdate.model = "GT-R"; carToFindForUpdate.horsepower = 550; carToFindForUpdate.cylinders = 8; //Save the changes to the database. carsYWangEntities.SaveChanges(); //========================================================================= //********************************************************************** //How to do an delete //**************************************************************** //Get a car out of the database that we would like to update Car carToFindForDelete = carsYWangEntities.Cars.Find("V0LCD1814"); //Remove the Car from the Cars table carsYWangEntities.Cars.Remove(carToFindForDelete); //Save the changes to the database. carsYWangEntities.SaveChanges(); }
static void Main(string[] args) { /* AdventureWorks2012Entities adventure = new AdventureWorks2012Entities(); int counter = 0; Guid id = Guid.NewGuid(); foreach (Person person in adventure.People) { if (counter == 0) { id = person.rowguid; } if (counter++ > 20) { break; } Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.rowguid); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(adventure.People.Find(id)); */ /* foreach (EmailAddress email in adventure.EmailAddresses) { Console.WriteLine(email); } */ //************************************************************ //List out all of the cars in the table //************************************************************ //Gets access to the collection of tables we can interact with. CarsTestEntities carsTestEntities = new CarsTestEntities(); Console.WriteLine("Print the list"); //Loop through all of the cars in the table called Cars. foreach (Car car in carsTestEntities.Cars) { Console.WriteLine(car.id + " " + car.make + " " + car.model); } //************************************************************* //Find a specific one by any property //************************************************************* //Call the Where method on the table Cars and pass in a lambda expression for //the criteria we are looking for. There is nothing special about the work car //in the part that reads: car => car.id == "v0...". It could be any characters we //want it to be. car made sense. Also, car represents the object we want to do //the where clause on. That's why we have car.id == "V0..". Car carToFind = carsTestEntities.Cars.Where(car => car.id == "V0LCD1814").First(); //We can look for a specific model from the database with a where clause based on any //criteria we want. Here we are doing it with the Car's model instead of it's id.. Car otherCarToFind = carsTestEntities.Cars.Where(car => car.model == "Challenger").First(); //Print them out. Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Find 2 specific cars"); Console.WriteLine(carToFind.id + " " + carToFind.make + " " + carToFind.model); Console.WriteLine(otherCarToFind.id + " " + otherCarToFind.make + " " + otherCarToFind.model); //************************************************************************************** //Find a car based on the primary Id //************************************************************************************** //Pull out a car from the table based on the primary Id Car foundCar = carsTestEntities.Cars.Find("V0LCD1814"); //Print it out Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Print out a found car using the Find method"); //TODO: uncomment when primary id is fixed Console.WriteLine(foundCar.id + " " + foundCar.make + " " + foundCar.model); //************************************************************************************* //Add a new Car to the database //************************************************************************************* //Make an instance of a new car Car newCarToAdd = new Car(); //Assign properties to the parts of the model newCarToAdd.id = "88888"; newCarToAdd.make = "Nissan"; newCarToAdd.model = "GT-R"; newCarToAdd.horsepower = 550; newCarToAdd.cylinders = 8; newCarToAdd.year = "2016"; newCarToAdd.type = "Car"; //Add the new car to the Cars table carsTestEntities.Cars.Add(newCarToAdd); //This method call actually does the work of saving the changes to the database carsTestEntities.SaveChanges(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Add a car and verify it is there"); carToFind = carsTestEntities.Cars.Find("88888"); Console.WriteLine(carToFind.id + " " + carToFind.make + " " + carToFind.model); //******************************************************************************* //How to do an update //******************************************************************************* //Get a car out of the database that we would like to update Car carToFindForUpdate = carsTestEntities.Cars.Find("V0LCD1814"); //Update some of the properties of the car we found. Don't need to update all of them //if we don't want to. I choose these 4. carToFindForUpdate.make = "Nissan"; carToFindForUpdate.model = "GT-R"; carToFindForUpdate.horsepower = 550; carToFindForUpdate.cylinders = 8; //Save the changes to the database carsTestEntities.SaveChanges(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Update a car with data"); Console.WriteLine(carToFindForUpdate.id + " " + carToFindForUpdate.make + " " + carToFindForUpdate.model); //******************************************************************************* //How to do a delete //******************************************************************************* //Get a car out of the database that we would like to update Car carToFindForDelete = carsTestEntities.Cars.Find("88888"); //Remove the Car from the Cars table carsTestEntities.Cars.Remove(carToFindForDelete); //Save the changes to the database carsTestEntities.SaveChanges(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Deleted the added car. Looking to see if it is still in the DB"); try { carToFindForDelete = carsTestEntities.Cars.Find("88888"); Console.WriteLine(carToFindForDelete.id + " " + carToFindForDelete.make + " " + carToFindForDelete.model); } catch (Exception e) { Console.WriteLine("The model you are looking for does not exist"); } }
static void Main(string[] args) { /* // Creates collection. AdventureWorks2012Entities adventure = new AdventureWorks2012Entities(); int counter = 0; foreach (Person person in adventure.People) { if (counter > 20) { break; } Console.WriteLine(person.FirstName + " " + person.LastName); counter++; } Console.WriteLine(); /* foreach (EmailAddress email in adventure.EmailAddresses) { Console.WriteLine(email); } * */ // Gets acces to the collection of tables to interact with. CarsDBEntities carsTestEntities = new CarsDBEntities(); // READ FROM DATABASE. Console.WriteLine(Environment.NewLine + Environment.NewLine); Console.WriteLine("Reading from DataBase" + Environment.NewLine); // Loop through table and print out for each. foreach (Car car in carsTestEntities.Cars) { Console.WriteLine(car.id + " " + car.make + " " + car.model); } // SEARCH THROUGH DATABASE. Console.WriteLine(Environment.NewLine + Environment.NewLine); Console.WriteLine("Searching through DataBase" + Environment.NewLine); // Using Lambdas? // Lambda (Made up variable. Does not matter what it is. => Search through id column => Expected result.) // Find a specific car by any property. (AnyVariable => AnyVariable.ThingToSearchOfVariable == "ExpectedResultHere").Returns the first one found which matches criteria. Car carToFind = carsTestEntities.Cars.Where(c => c.id == "V0LCD1814").First(); Car otherCarToFind = carsTestEntities.Cars.Where(asdf => asdf.model == "Challenger").First(); // Prints out to console. Console.WriteLine(carToFind.id + " " + carToFind.make + " " + carToFind.model); Console.WriteLine(otherCarToFind.id + " " + otherCarToFind.make + " " + otherCarToFind.model); Console.WriteLine(Environment.NewLine + Environment.NewLine); // Pull out a car from table based on primary id. Car foundCar = carsTestEntities.Cars.Find("V0LCD1814"); // Print out. Console.WriteLine(foundCar.id + " " + foundCar.make + " " + foundCar.model); // ADD TO DATABASE. Console.WriteLine(Environment.NewLine + Environment.NewLine); Console.WriteLine("Add to DataBase" + Environment.NewLine); // Make new instance of car. Car newCarTooAdd = new Car(); // Assign values to car. newCarTooAdd.id = "888888"; newCarTooAdd.make = "Nissan"; newCarTooAdd.model = "GT-R"; newCarTooAdd.horsepower = 550; newCarTooAdd.cylinders = 8; newCarTooAdd.year = "2016"; newCarTooAdd.type = "Car"; // Adds the new car to database table carsTestEntities.Cars.Add(newCarTooAdd); // Save changes to database. Data is not actually saved before this method. carsTestEntities.SaveChanges(); // UPDATE DATABASE. Console.WriteLine(Environment.NewLine + Environment.NewLine); Console.WriteLine("Update DataBase" + Environment.NewLine); // Get car out of database that we want to update. Car carToFindForUpdate = carsTestEntities.Cars.Find("V0LCD1814"); // Update some properties of car. Only need to change the ones we want to update. carToFindForUpdate.make = "Nissan"; carToFindForUpdate.model = "GT-R"; carToFindForUpdate.horsepower = 550; carToFindForUpdate.cylinders = 8; // Save the changes to database. carsTestEntities.SaveChanges(); // DELETE FROM DATABASE. Console.WriteLine(Environment.NewLine + Environment.NewLine); Console.WriteLine("Delete from DataBase" + Environment.NewLine); // Get car out of database to delete. Car carToFindForDelete = carsTestEntities.Cars.Find("V0LCD1814"); // Remove from table. carsTestEntities.Cars.Remove(carToFindForDelete); // Save the changes to database. carsTestEntities.SaveChanges(); }
static void Main(string[] args) { //Gets access to the collection of tables we can interact with. CarsJHarveyEntities1 carsJHarveyEntities = new CarsJHarveyEntities1(); //***************************************************************************// //Loop through all of the cars in the table called Cars. foreach (Car car in carsJHarveyEntities.Cars) { Console.WriteLine(car.id + " " + car.make + " " + car.model); } //***************************************************************************// //***************************************************************************// //Find a specific one by any property. //Call the where method on the table Cars and pass in a lambde expression for the criteria we are looking for. Car carToFind = carsJHarveyEntities.Cars.Where(c => c.id == "V0LCD1814").First(); //We can look for a specific model from the database with a where clause based on any criteria we want. Car otherCarToFind = carsJHarveyEntities.Cars.Where(car => car.model == "Challenger").First(); //Print them out. Console.WriteLine(); Console.WriteLine(); Console.WriteLine(carToFind.id + " " + carToFind.make + " " + carToFind.model); Console.WriteLine(otherCarToFind.id + " " + otherCarToFind.make + " " + otherCarToFind.model); //***************************************************************************// //***************************************************************************// //Find a car based on the primary Id //NOTE: This currently doesnt work because there are no primary Ids set on the tables. Car foundCar = carsJHarveyEntities.Cars.Find("V0LCD1814"); //Print that shit out. Console.WriteLine(); Console.WriteLine(); Console.WriteLine(foundCar.id + " " + foundCar.make + " " + foundCar.model); //***************************************************************************// //***************************************************************************// //NOTE: This currently doesnt work because there are no primary Ids set on the tables. //Add a new Car to the database. //Make an instance. Car newCarToAdd = new Car(); //Assign properties to the parts of the model. newCarToAdd.id = "88888"; newCarToAdd.make = "Nissan"; newCarToAdd.model = "GT-R"; newCarToAdd.horsepower = 550; newCarToAdd.cylinders = 8; newCarToAdd.year = "2016"; newCarToAdd.type = "Car"; //Add new car to the Cars table. carsJHarveyEntities.Cars.Add(newCarToAdd); //This method will save the changes to the database. carsJHarveyEntities.SaveChanges(); //***************************************************************************// //***************************************************************************// //Get a car out of the database that we would like to update Car carToFindForUpdate = carsJHarveyEntities.Cars.Find("V0LCD1814"); //Update some of the properties of the car we found. Dont need to update all of them if we dont want to. carToFindForUpdate.make = "Nissan"; carToFindForUpdate.model = "GT-R"; carToFindForUpdate.horsepower = 550; carToFindForUpdate.cylinders = 8; //Save changes to the database. carsJHarveyEntities.SaveChanges(); //******************************************************************************* //How to do a delete //******************************************************************************* //Get a car out of the database that we would like to update Car carToFindForDelete = carsJHarveyEntities.Cars.Find("88888"); //Remove the Car from the Cars table carsJHarveyEntities.Cars.Remove(carToFindForDelete); //Save the changes to the database carsJHarveyEntities.SaveChanges(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Deleted the added car. Looking to see if it is still in the DB"); try { carToFindForDelete = carsJHarveyEntities.Cars.Find("88888"); Console.WriteLine(carToFindForDelete.id + " " + carToFindForDelete.make + " " + carToFindForDelete.model); } catch (Exception e) { Console.WriteLine("The model you are looking for does not exist"); } Console.ReadKey(); /* AdventureWorks2012Entities adventure = new AdventureWorks2012Entities(); int counter = 0; Guid id = new Guid(); foreach (Person person in adventure.People) { if (counter == 0) { id = person.rowguid; } if (counter++ > 20) break; Console.WriteLine(person.FirstName + " " + person.LastName + " " + person.rowguid); } Console.WriteLine(); Console.WriteLine("----------------------------------------"); Console.WriteLine(adventure.People.Find(id)); foreach (EmailAddress email in adventure.EmailAddresses) { Console.WriteLine(email); } */ }