コード例 #1
0
        /// <summary>
        /// Returns all customers from the database
        /// </summary>
        /// <returns></returns>
        public static List <Customer> GetCustomers()
        {
            // Create instance of DB Context
            var dbc = new BookRegistrationEntities();

            // Use DB Context to retreive all customers
            // Use LINQ (Language INtegrated Query) to query database

            // LINQ Query Syntax
            //List<Customer> customers =
            //    (from row in dbc.Customer
            //         where row.LastName == "Reynolds"
            //         orderby row.LastName
            //     select row).ToList();

            //return customers;

            // LINQ Method Syntax - Same query as above
            List <Customer> customers =
                dbc.Customer
                //.Where(row => row.LastName == "Reynolds")
                //.OrderByDescending(row => row.LastName)
                .ToList();

            return(customers);
        }
コード例 #2
0
        public static List <Book> GetAllBooks()
        {
            BookRegistrationEntities context = new BookRegistrationEntities();
            List <Book> allBooks             = (from b in context.Book select b).ToList();

            return(allBooks);
        }
コード例 #3
0
        public static List <Customer> GetAllCustomers()
        {
            BookRegistrationEntities context   = new BookRegistrationEntities();
            List <Customer>          customers = context.Customer.ToList();

            return(customers);
        }
コード例 #4
0
        /// <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.
            // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement
            using (var context = new BookRegistrationEntities())
            {
                // LINQ Method Syntax
                return(context
                       .Book
                       .OrderBy(book => book.Title)
                       .ToList());

                // LINQ Query Syntax

                /*
                 * List<Book> books =
                 *  (from row in context.Book
                 *   orderby row.Title ascending
                 *   select row).ToList();
                 *
                 * return books;
                 */

                // Ditto - without variable.
                // A bit unclear about return type though.

                /*
                 * return
                 *  (from row in context.Book
                 *   orderby row.Title ascending
                 *   select row).ToList();
                 */
            }
        }
コード例 #5
0
 public static void DeleteCustomer(Customer c)
 {
     using (var context = new BookRegistrationEntities())
     {
         context.Customer.Add(c);                      // "Track" with EF
         context.Entry(c).State = EntityState.Deleted; // Tell EF we are removing it
         int rowsAffected = context.SaveChanges();     // Update on database
         // int rowsAffected is optional
     }
 }
コード例 #6
0
 public static Customer UpdateCustomer(Customer c)
 {
     using (var context = new BookRegistrationEntities())
     {
         context.Customer.Add(c);                       // Add to context
         // Tell EF we are updating an existing entity
         context.Entry(c).State = EntityState.Modified; // Tell EF it has been modified
         context.SaveChanges();
         return(c);
     }
 }
コード例 #7
0
        /// <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 populated
                return(c);
            }
        }