Check the EDM of the database by double clicking on 'School.edmx' in 'Solution Explorer'. You may also check the EDMX file by opening it in the XML Editor.
The next step is to query the conceptual model or EDM (entity data model) model that we have just created. This can be done by adding the following controls to our form: DataGridView and ComboBox, and two buttons. Here DataGridView and ComboBox are for displaying data from EDM. One button is to update changes made to EDM back to actual database and the other is used to close the application. Here's the sample code:
using System.Data.Objects; using System.Data.Objects.DataClasses; namespace PCQLADOEF { public partial class Form1 : Form { private SchoolEntities schoolContext; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { schoolContext = new SchoolEntities(); ObjectQuery departmentQuery = schoolContext.Department.Include("Course").OrderBy("it.Name"); try { this.comboBox1.DataSource = departmentQuery; this.comboBox1.DisplayMember = "Name"; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Here's the code for 'button1.' It closes the form and disconnects connection to the EDM:
Here's the output of our application. Select department from combo box and then edit values. To update these values click on 'button2'. 'Button1' closes the app.
private void button1_Click(object sender, EventArgs e) { this.Close(); schoolContext.Dispose(); }
To populate data from EDM to data grid of the form, the following code is written:
private void com- boBox1_SelectedIndexChanged(object sender, EventArgs e) { try { Department department = (Department)this.comboBox1.SelectedItem; dataGridView1.DataSource = department.Course; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Finally the changes made to EDM need to be translated back to actual database, this is done by writing the following code on click event of 'button2':
To see the details of 'School.edmx' file, right click on it and open it in 'XML Editor'. The XML file will show three parts: Conceptual Model, Storage Model and Mappings.
private void button2_Click(object sender, EventArgs e) { try { int numChanges; numChanges = schoolContext.SaveChanges(); MessageBox.Show(numChanges.ToString() + " change(s) saved to the database."); this.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
With ADO.NET Entity Framework programming is simplified with the introduction of a new layer between actual database and application. The actual database here can only be SQL Server and not any other database; this can be seen as limiting factor though there are many open source APIs available for other databases.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.