Entity Framework Walk Through

  1. Fire up Visual Studio 2010
  2. Create a new project called EFExample
  3. If Data Sources are not visible, MENU: Data, Show Data Sources.
  4. Create a new class library project called EFDataModel within your solution
  5. Delete the Class1.cs file that is created by default, and add a new ADO.NET Entity Data Model
    1. Name: Sample.edmx
    2. Generate form database
    3. Select or create a new connection to your sample database. Next
    4. Select all the tables, Finish
  6. After you save the Sample.edmx file, build your project (ctrl+shift+b)
  7. Copy the app.config file from EFDataModel to the EFExample project (This just gets the connection string into the main program)
  8. Right click on the EFExample project and add a reference to the EFDataModel project. (WE DO NEED THIS OR We need to create a connection string for the application)
  9. Also add a reference to System.Data.Entity

Ok, we are done with the preliminaries. Go to Form1 and edit the Load event handler (just double click on the surface of the form). Enter the following code:

   1         private void Form1_Load(object sender, EventArgs e)
   2         {
   3             try
   4             {
   5                 using (var context = new CPTR319_SampleEntities())
   6                 {
   7                     var students = context.students;
   8                     StringBuilder s = new StringBuilder();
   9                     foreach (var st in students)
  10                     {
  11                         s.Append(st.name + "\r\n");
  12                     }
  13                     MessageBox.Show(s.ToString());
  14                 }
  15             }
  16             catch (Exception ex)
  17             {
  18                 MessageBox.Show(ex.Message);
  19             }
  20 
  21         }

Ok, so that's not very useful. So lets show data on the form.

  1. Select the form and then click on Add Data Sources (Menu: Data, Add new Data source...)
  2. Choose Object next,
  3. Select EFDataModel, Finish
  4. Using the Data Sources create a form that looks like the following:

Form1.png

Now we need to do a little coding or the form will not show anything! The first variable below will have form wide scope and should be declared at the top of the class. Change the form load event to be the following:

   1         //new variable 
   2         CPTR319_SampleEntities _context;
   3 
   4         //...
   5 
   6 
   7         private void Form1_Load(object sender, EventArgs e)
   8         {
   9             try
  10             {
  11                 _context = new CPTR319_SampleEntities();
  12                 var students = _context.students.Include("takes");
  13                 studentBindingSource.DataSource = students.ToList();                
  14             }
  15             catch (Exception ex)
  16             {
  17                 MessageBox.Show(ex.Message);
  18             }
  19 
  20         }

Now run the form and view the data. What happens when you try to change some of your data?

To fix this problem, we need to add a save method to the binding navigator.

  1. Right click the save icon and click enable in the form designer.
  2. Select the save button and view the event handlers.
  3. Double click on the Click event.
  4. Add the single line to the event handler method: _context.SaveChanges();

You should now be able to save data and you don't have to be on the changed record to do so. What happens if we are in the middle of editing a record? How would you fix it? (I'm leaving that little exercise to you).

Download Example

DatabaseManagementSystems/EntityFrameworkExample (last edited 2013-02-14 23:58:46 by 24-151-197-61)