⇤ ← Revision 1 as of 2011-02-14 01:25:38
1798
Comment:
|
2094
|
Deletions are marked like this. | Additions are marked like this. |
Line 43: | Line 43: |
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...) 1. Choose Object next, 1. Select EFDataModel, Finish 1. Using the Data Sources create a form that looks like the following: |
Entity Framework Walk Through
- Fire up Visual Studio 2010
- Create a new project called EFExample
- If Data Sources are not visible, MENU: Data, Show Data Sources.
- Create a new class library project called EFDataModel within your solution
- Delete the Class1.cs file that is created by default, and add a new ADO.NET Entity Data Model
- Name: Sample.edmx b. Generate form database c. Select or create a new connection to your sample database. Next d. Select all the tables, Finish
- After you save the Sample.edmx file, build your project (ctrl+shift+b)
- Copy the app.config file from EFDataModel to the EFExample project (This just gets the connection string into the main program)
- 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)
- 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.
- Select the form and then click on Add Data Sources (Menu: Data, Add new Data source...)
- Choose Object next,
- Select EFDataModel, Finish
- Using the Data Sources create a form that looks like the following: