OLD Entity Framework Data Binding

  1. In a nutshell, how is data-binding performed on a webpage using EF to perform basic CRUD operations? Why is this important?
    • Without a single line of code being written, a developer can attach to a database, build a data model based on the database structure (classes), and has all the necessary structures needed to support CRUD operations.
    • While EF can do all this without a line of code be written by the developer, it surely generates massive amounts in the code behind, however, it is very organized and structured which creates a clean separation between the┬ádata and it's objects (data model classes). Binding ties the UI to the EF either directly or indirectly through business objects. Commonly a GridView is used for this and a few simple CRUD events can be fired through the GridView and then tied to the underlying database using EF, such as with (ie, SelectMethod and ItemType).
  2. What events are triggered for each of the 4 CRUD events?
    • SelectMethod="GridView1_GetData()"
    • UpdateMethod="GridView1_UpdateItem()"
    • DeleteMethod="GridView1_DeleteItem()"
    • InsertMethod="DetailsView1_InsertItem()"
  3. Write the function for SelectMethod.
    • // The return type can be changed to IEnumerable, however to support paging and sorting, the following parameters
    • // must be added: int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression
    • public IQueryable<EFGridViewExample.Course> GridView1_GetData()
    • {
    • // use our EF table and filter, then massage results (order by) using linq
    • return new EducationDBEntities().Courses.Where(i => i.Year >= 2010).OrderBy(i => i.Class);

    • // several varitations exist on how we want to pull our data and how we want to display it
    • // below is a longer way of doing the same thing, first creating our context course table and then
    • // poulating it, and then filtering using linq before passing it back to user.
    • // var db = new EducationDBEntities().Courses;
    • // var result = (from c in db
    • // orderby c.Id ascending
    • // select c).Take(100);
    • //return result;
    • }
  4. Write the function for UpdateMethod.
    • public void GridView1_UpdateItem(int id)
    • {
    • // populate our data model class (course) with the record we found
    • var db = new EducationDBEntities();
    • EFGridViewExample.Course item = db.Courses.Find(id);

    • // make sure we found the error, otherwise display the error to the user
    • if (item == null)
    • {
    • ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
    • return;
    • }
    • TryUpdateModel(item);
    • if (ModelState.IsValid)
    • {
    • db.SaveChanges();
    • GridView1.DataBind();
    • }
    • }
  5. Write the function for DeleteMethod.
    • public void GridView1_DeleteItem(int id)
    • {
    • // other slight variations of grabbing the first records exist including: .First(),
    • // .FirstOrDefault(), .Single(), .SingleOrDefault(), and .Find(). Find is based on
    • // primary key and can perform typically faster than others, ie: var rec = db.Courses.Find(id);
    • var db = new EducationDBEntities();
    • var course = db.Courses.Where(i => i.Id == id).FirstOrDefault();

    • if (course == null)
    • {
    • ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
    • return;
    • }
    • else
    • {
    • db.Courses.Remove(course);
    • db.SaveChanges();
    • GridView1.DataBind();
    • }
    • }
  6. Write the function for InsertMethod.
    • // below we use the framework generated values to mesh our values directly to the
    • // class EF generated for us. We could have also manually done this using the following:
    • // var course = new Course() { Id = 99, Class = "Yoga for Beginners", Year = 2014, Semester = "Fall" };
    • public void DetailsView1_InsertItem()
    • {
    • // initialize a blank course object (strings are null, numeric values 0, virtual table counts 0)
    • var course = new EFGridViewExample.Course();

    • // mesh the class and the user input together
    • TryUpdateModel(course);
    • if (ModelState.IsValid)
    • {
    • // Save changes here
    • using (var db = new EducationDBEntities())
    • {
    • db.Courses.Add(course);
    • db.SaveChanges();
    • GridView1.DataBind();
    • }
    • }
    • }
Author
mateotete
ID
335113
Card Set
OLD Entity Framework Data Binding
Description
Questions on Entity Framework data binding to GridView.
Updated