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(); }
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. }
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. } }