Using MySQL With Entity Framework

Introduction

 
Entity Framework is the next level of database programming which is much more flexible and adaptable than earlier methods like Regular ADO.Net and LINQ to SQL.
 
Prerequisites
  • Install Visual Studio 2008 or Visual Studio 2010
  • Install MySQL database on your local machine
  • MySQL database admin tool that allows you to create a database and run SQL statements. I am using phpMyAdmin which is a web interface.
  • Download and install the MySQL Connector.
Getting Started
 
Run the XAMPP application and it will automatically install the Apache server, MySQL database, and FileZilla. After installing check whether these services are running or not. The following XAMPP control panel shows which of those services are currently running.
 
p1.bmp
 
Now the following steps will show how to connect to a MySQL database using C#.
 
Step 1: Open the MySQL Admin page and create a new database.
 
p2.bmp
 
Step 2: After creating the new database, create a new table.
  1. CREATE TABLE `employee`(  
  2.     `EmpId` bigint(20) NOT NULL,  
  3.     `EmpName`varchar(200) default NULL,  
  4.     `EmpAddress` varchar(255) default NULL,  
  5.     PRIMARY KEY(`EmpId `)  
  6. ) ENGINE = InnoDB DEFAULT CHARSET = latin1; 
Step 3: After creating the new table, open Visual Studio, and click on New Project and name the project. It will open the new project, then click on Solution Explorer (F4), right-click on "Reference" to add a new reference to the project. Reference those two .dll files to the project (MySql.dll (Win apps), MySql.Data.Entity.dll).
 
p3.bmp
 
Step 4: In Solution Explorer go to the "System.Data" properties and set the "Copy Local" property to true.
 
The Copy Local property (corresponding to CopyLocal) determines whether a reference is copied to the local bin path. At run time, a reference must exist in either the Global Assembly Cache (GAC) or the output path of the project. If this property is set to true, the reference is copied to the output path of the project at run time.
 
p4.bmp
 
Step 5: Add a new Entity Data Model into the project.
 
p5.bmp
 
Step 6: Select "Generate from database" in the Entity Data Model Wizard.
 
p6.bmp
 
Step 7: Select MySQL Database.
 
p7.bmp
 
Step 8: Set Connection properties for MySQL.
 
p8.bmp
 
Step 9: This step will display all the tables, views, and stored procedures.
 
p9.bmp
 
Step 10: By double-clicking on Model1.edmx, it will open the EDM designer which displays all the entities for selected tables.
 
p10.bmp
 
Step 11: The following code will insert the data into the MySQL table.
  1. private void btnInsert_Click(object sender, EventArgs e) {  
  2.   testdbEntities testcontext = new testdbEntities();  
  3.   try {  
  4.     employee emp = new employee {  
  5.       EmpId = int.Parse(txtId.Text),  
  6.         EmpName = txtName.Text,  
  7.         EmpAddress = txtAddress.Text  
  8.     };  
  9.     testcontext.employee.AddObject(emp);  
  10.     testcontext.SaveChanges();  
  11.     MessageBox.Show("Record Inserted successfully.""Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  12.     LoadToGrid();  
  13.   } catch (Exception ex) {  
  14.     MessageBox.Show(ex.InnerException.ToString());  
  15.   }  
Step 12: The following code will update the data in the MySQL table.
  1. private void btnUpdate_Click(object sender, EventArgs e) {  
  2.     int EmpId;  
  3.     string s = txtId.Text;  
  4.     int.TryParse(s, out EmpId);  
  5.     testdbEntities testcontext = new testdbEntities();  
  6.     try {  
  7.         employee emp = testcontext.employee.First(i => i.EmpId == EmpId); {  
  8.             emp.EmpName = txtName.Text;  
  9.             emp.EmpAddress = txtAddress.Text;  
  10.             testcontext.SaveChanges();  
  11.             MessageBox.Show("Record Updated successfully.""Update", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  12.             LoadToGrid();  
  13.         };  
  14.     } catch (Exception ex) {  
  15.         MessageBox.Show(ex.InnerException.ToString());  
  16.     }  
Step 13: The following code will delete the data into the MySQL table.
  1. private void btnDelete_Click(object sender, EventArgs e) {  
  2.     int EmpId;  
  3.     string s = txtId.Text;  
  4.     int.TryParse(s, out EmpId);  
  5.     testdbEntities testcontext = new testdbEntities();  
  6.     try {  
  7.         employee emp = testcontext.employee.First(i => i.EmpId == EmpId);  
  8.         testcontext.employee.DeleteObject(emp);  
  9.         testcontext.SaveChanges();  
  10.         MessageBox.Show("Record Deleted successfully.""Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  11.         LoadToGrid();  
  12.     } catch (Exception ex) {  
  13.         MessageBox.Show(ex.InnerException.ToString());  
  14.     }  
Step 14: The following function will load the data from the table and bind it to a GridView. 
  1. private void LoadToGrid() {  
  2.     testdbEntities testcontext = new testdbEntities();  
  3.     var load = from g in testcontext.employee select g;  
  4.     if (load != null) {  
  5.         dataGridView1.DataSource = load.ToList();  
  6.     }  
Step 15: Final Result
 
p11.bmp
 


Similar Articles