/// <summary> /// Returns all customers from the database /// </summary> /// <returns></returns> public static List <Customer> GetCustomers() { // Create instance of DB Context var db = new BookRegistrationEntities(); // Use DB Context to retrieve all customers // Use LINQ (Language Integrated Query) ----- to query database. // ^ language instead of c# to communicate with database. // LINQ query syntax List <Customer> customers = (from c in db.Customer //where c.LastName == "Ortiz" //orderby c.LastName select c).ToList(); // LINQ method syntax - Same as above but slightly different syntax //List<Customer> customers = // db.Customer // .Where(c => c.LastName == "Ortiz") // .OrderByDescending(c => c.LastName) // .ToList(); return(customers); }
/// <summary> /// Retrieves all books sorted in /// alphabetical by title /// </summary> /// <returns></returns> public static List <Book> GetBooks() { // The using statement will force the compiler to // create a try-finally. the finally will dispose of database context. using (var context = new BookRegistrationEntities()) { // LINQ Method Syntax /* * return context * .Book * .OrderBy(b => b.Title) * .ToList(); */ // LINQ Query Syntax List <Book> books = ( from b in context.Book orderby b.Title ascending select b ).ToList(); return(books); } }
/// <summary> /// Retrieves all books sorted in alphabetical order by title /// </summary> /// <returns></returns> public static List <Book> GetBooks() { //The using statement will forve the compiler to create a try/finally //Disposes of it when finished using (var context = new BookRegistrationEntities()) { return(context.Book.OrderBy(b => b.Title).ToList()); } }
/// <summary> /// Add's a customer. Return's newly added customer /// with the CustomerId populated. /// </summary> /// <param name="c">New Customer to be added.</param> /// <returns></returns> public static Customer AddCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); context.SaveChanges(); return c; } }
public static void DeleteCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); context.Entry(c).State = EntityState.Deleted; int rowsAffecteed = context.SaveChanges(); } }
public static Customer UpdateCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); context.Entry(c).State = EntityState.Modified; context.SaveChanges(); return c; } }
/// <summary> /// Adds a customer. Returns the newly added customer with the CustomerID populated. /// </summary> /// <param name="c">The new customer</param> /// <returns></returns> public static Customer AddCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); //save changes MUST be called with inserts, updates, and deletes context.SaveChanges(); return(c); } }
/// <summary> /// Adds a customer. Returns the newly added customer with the CustomerId populated /// </summary> /// <param name="c">The new Customer to be added</param> /// <returns></returns> public static Customer AddCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); context.SaveChanges(); // Return newly added customer with CustomerID populated return(c); } }
public static Customer UpdateCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); // Tell EF we are updating an existing entity context.Entry(c).State = EntityState.Modified; context.SaveChanges(); return(c); } }
/// <summary> /// Adds a customer. Returns the newly added customer with the Customer Id populated /// </summary> /// <param name="c">The new Customer to be added</param> /// <returns></returns> public static Customer AddCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); //Save changes must be called for INSERTS UPDATES and DELETES context.SaveChanges(); //Return newly added customer with CustomerId(Identity column) populated return(c); } }
/// <summary> /// Adds a customer. Returns the newly added customer with the CustomerId populated. /// </summary> /// <param name="c">The new customer to be added.</param> /// <returns></returns> public static Customer AddCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); // Save changes must be called for insert/update/delete context.SaveChanges(); // Return newly added customer with customer id populated. return(c); } }
/// <summary> /// Adds a customer. Returns the newly added customer /// with the CustomerId populated /// </summary> /// <param name="c">The new Customer to be added</param> /// <returns></returns> public static Customer AddCustomer(Customer c) { using (var context = new BookRegistrationEntities()) { context.Customer.Add(c); //SaveChanges MUST BE CALLED FOR insert/update/delete context.SaveChanges(); //Return newly added customer with CustomerId (Identity column) populated return(c); } }
/// <summary> /// Retrieves all books sorted in alphabetical by title /// </summary> /// <returns></returns> public static List <Book> GetBooks() { // The using statement will force the compiler to create a try/finally . the finally will dispose of the db context using (var context = new BookRegistrationEntities()) { //LINQ Query Syntax List <Book> books = (from b in context.Book orderby b.Title ascending select b).ToList(); return(books); } }
public static Customer UpdateCustomer(Customer c) { // If a customer ID = 0 EF will know this is a new customer. using (var context = new BookRegistrationEntities()) { //Adds to context, not DB(technically) so it starts tracking it context.Customer.Add(c); //Tell EF we are updating an existing entity context.Entry(c).State = EntityState.Modified; context.SaveChanges(); return(c); } }
/// <summary> /// Returns all customers from the database /// </summary> /// <returns></returns> public static List <Customer> GetCustomers() { //Create instance of DB context //(what is talking to your database) var db = new BookRegistrationEntities(); //Use linq (language integrated query) to query database //LINQ Query syntax //List<Customer> customers = (from c in db.Customer // select c).ToList(); //LINQ method syntax - Same as above List <Customer> customers = db.Customer.ToList(); return(customers); }
/// <summary> /// Returns all customers from the database /// </summary> /// <returns></returns> public static List <Customer> GetCustomers() { // Create instance of DB Context var db = new BookRegistrationEntities(); // Use DB Context to retrieve all customers // Use LINQ (Language Integrated Query) // to query database // LINQ Query Syntax //List<Customer> customers = (from c in db.Customer select c).ToList(); // LINQ Method Syntax - Same query as above List <Customer> customers = db.Customer.ToList(); return(customers); }
/// <summary> /// Retrieves all books, sorted, and in alphabetical order by title(A-Z) /// </summary> /// <returns></returns> public static List <Book> GetBooks() { // the using statement will force the compiler to create a try/finally // The finally will dispose of the db context. // SEARCH: using statement for documentation. using (var context = new BookRegistrationEntities()) { // Can also reference LINQ Method Syntax. Example in CustomerDb.cs // LINQ query syntax (Database Queries) List <Book> books = (from b in context.Book orderby b.Title ascending select b).ToList(); return(books); // you can also get rid of the variable name and just return the actual query. } }
/// <summary> /// Returns all Customers from the data base /// </summary> /// <returns></returns> public static List <Customer> GetCustomers() { //Create instance of DB context var db = new BookRegistrationEntities(); //Use DB context to retrieve all Customers //Use LINQ to query database(cast to list) //Query syntax //List<Customer> customers = // (from c in db.Customer // select c).ToList(); //LINQ Method Syntax -Same query as above List <Customer> customers = db.Customer .ToList(); return(customers); }
/// <summary> /// Retrieves all books sorted in alphabetical order by title. /// </summary> /// <returns></returns> public static List <Book> GetBooks() { //The using statement will force the compiler to create a try/finally. The finally // will dispose of the db context using (var context = new BookRegistrationEntities()) { //LINQ Method Syntax return(context .Book .OrderBy(b => b.Title) .ToList()); //LINQ Query Syntax //List<Book> books = // (from b in context.Book // orderby b.Title ascending // select b).ToList(); //return books; } }
/// <summary> /// Return's all customers from the database. /// </summary> /// <returns></returns> public static List<Customer> GetCustomers() { // Create instance of DB Context var db = new BookRegistrationEntities(); // Use Db context to retrieve all customers /* List<Customer> customers = ( from c in db.Customer where c.LastName == "Disney" orderby c.LastName descending select c ).ToList<Customer>(); */ List<Customer> customers = db.Customer.ToList(); return customers; }