Example #1
0
 private void btnEFDeletingData_Click(object sender, EventArgs e)
 {
     using (var ctx = new NorthwindsEntities())
     {
         Categories cat = ctx.Categories.First(c => c.CategoryName == "teste1");
         ctx.Categories.Remove(cat);
         ctx.SaveChanges();
     }
     // You can also delete records by using just a few lines of code.
     // using (NorthwindsEntities db = new NorthwindsEntities())
     //            {
     //                Category category = db.Categories.First(c => c.CategoryName == "Alcohol");
     //                db.Categories.Remove(category);
     //                db.SaveChanges();
     //            }
     // In Entity Framework 5.0 you use the Remove method.In previous versions the method was called
     // DeleteObject.
 }
Example #2
0
        private void btnEFStoredProcedures_Click(object sender, EventArgs e)
        {
            using (var ctx = new NorthwindsEntities())
            {
                var queryFromProcedure = ctx.CustOrderHist("asdasdasd");
                foreach (CustOrderHist_Result res in queryFromProcedure)
                {
                    Debug.WriteLine(string.Format("Product Name; {0}", res.ProductName));
                }
            }

            // Call a Stored Procedure
            // As previously shown, all the stored procedures were created as methods in the NorthwindsEntities
            // class by the Entity Data Model Wizard.To call a stored procedure, you simply need to call the method.
            // The following code sample calls the CustOrderHist stored procedure, passes in a customer ID, and
            // then prints the orders to the Output window:
            // using (NorthwindsEntities db = new NorthwindsEntities())
            // {
            //     var custOrderHist = db.CustOrderHist("ALFKI");
            //     foreach (CustOrderHist_Result result in custOrderHist)
            //     {
            //         Debug.WriteLine(string.Format("ProductName: {0}, Total: {1}",
            //                                       result.ProductName, result.Total));
            //     }
            // }
            // As you can see, all the heavy lifting is done for you by the Entity Framework, but it is still important to
            // understand what is going on behind the scenes with ADO.NET to become a more complete developer.
        }
Example #3
0
        private void btnEFUpdatingData_Click(object sender, EventArgs e)
        {
            using (var ctx = new NorthwindsEntities())
            {
                Categories cat = ctx.Categories.First(c => c.CategoryName == "Produce");
                cat.Description = "Dried fruit and bean curd - modified";
                ctx.SaveChanges();
            }

            //            Updating records is just as trivial.The following code sample retrieves the Category with the name
            //           Alcohol, changes its description, and then updates the record in the database:
            //Category category = db.Categories.First(c => c.CategoryName == "Alcohol");
            //            category.Description = "Happy People";
            //            db.SaveChanges();

        }
Example #4
0
 private void btnEFInsertingData_Click(object sender, EventArgs e)
 {
     using (var ctx = new NorthwindsEntities())
     {
         Categories cat = new Categories();
         cat.CategoryName = "teste1";
         cat.Description = "Test Description";
         //
         ctx.Categories.Add(cat);
         ctx.SaveChanges();
         //
         // This code created an instance of the Category class and initialized its properties. It then added
         // the object to the Categories property of the NorthwindsEntities.The SaveChanges() method 
         // is then called to add the record to the database. Again, there was no SQL syntax needed; the Entity
         // Framework handled all that behind the scenes.
     }
 }
Example #5
0
        private void EF_Tips_Selecting_Data_From_Db_Using_Linq_Joining()
        {
            using (var ctx = new NorthwindsEntities())
            {
                var products =

                    from
                        c in ctx.Categories
                    join
                        p in ctx.Products on c.CategoryID equals p.CategoryID
                    select p;

                foreach (Products prod in products)
                {
                    Debug.WriteLine(string.Format("ProductName: {0}", prod.ProductName));
                }

            }

        }
Example #6
0
        private void EF_Tips_Selecting_Data_From_Db_Using_Linq()
        {
            using (var ctx = new NorthwindsEntities())
            {
                var categories = from c in ctx.Categories select c;
                foreach (var cat in categories)
                {
                    Debug.WriteLine(string.Format("Id d acategoria: {0}", cat.CategoryID.ToString()));
                }
            }

        }
Example #7
0
 private void EF_Tips_Selecting_Data_From_Db_Using_ToList()
 {
     using (var ctx = new NorthwindsEntities())
     {
         var categories = ctx.Categories.ToList();
         foreach (Categories cat in categories)
         {
             Debug.WriteLine(cat);
         }
     }
 }