In this article I will explain a simple step by step tutorial with an example, how to use and connect to MySQL Database with
Entity Framework in ASP.Net MVC Razor.
By default,
Entity Framework cannot connect to MySQL Database and hence
MySql Entity Framework Connector needs to be downloaded.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Installing and adding reference of MySQL Entity Framework Connector
In order to install and add reference of MySql Entity Framework Connector, you will need to:-
1. Right Click the Project in Solution Explorer and click Manage NuGet Packages from the Context Menu.
2. Now you will need to look for MySql.Data.Entity package and once found, you need to click the Install Button.
Configuring and connecting Entity Framework to MySQL database
Now I will explain the steps to configure and add
Entity Framework and also how to connect it with the database.
You will need to add Entity Data Model to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as CustomersModel and then click Add button.
Then the Entity Data Model Wizard will open up where you need to select EF Designer database option.
Now the wizard will ask you to connect and configure the Connection String to the database. You will need to click on the New Connection button.
In the next dialog, you will need to click on the Change button in order to select MySQL Data Provider.
From the following window, you will need to select the MySQL Database as Data Source and click OK button.
Then in the following window, you will be asked to supply the Connection Details for the MySQL database.
Once the Connection String is generated, click Next button to move to the next step.
Next you will need to choose the
Entity Framework version to be used for connection.
Now you will need to choose the
Tables you need to connect and work with
Entity Framework. Here
Customers Table is selected.
The above was the last step and you should now have the Entity Data Model ready with the Customers Table.
Controller
The
Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the
Customers Table of the MySQL Database.
Inside the Index Action method, the Customer records are fetched using
Entity Framework and returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
CustomersEntities entites = new CustomersEntities();
return View(entites.customers.ToList());
}
}
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that the Model will be available as a Collection.
The
WebGrid is initialized with the Model i.e.
IEnumerable collection of
Customer Entity class objects as source.
The
WebGrid is created using the
GetHtml method with the following parameters.
HtmlAttributes – It is used to set the HTML attributes to the HTML Table generated by
WebGrid such as ID, Name, Class, etc.
Columns – It is used to specify the columns to be displayed in
WebGrid and also allows to set specific
Header Text for the columns.
Note: If the columns are not specified then
WebGrid will display all columns supplied by its source. It is very similar to the
AutoGenerateColumns feature of the ASP.Net GridView.
@model IEnumerable<customer>
@{
Layout = null;
WebGrid webGrid = new WebGrid(source: Model, canSort: false, canPage: false);
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
.Grid {
border: 1px solid #ccc;
border-collapse: collapse;
}
.Grid th {
background-color: #F7F7F7;
font-weight: bold;
}
.Grid th, .Grid td {
padding: 5px;
border: 1px solid #ccc;
}
.Grid, .Grid table td {
border: 0px solid #ccc;
}
</style>
</head>
<body>
@webGrid.GetHtml(
htmlAttributes: new { @id = "WebGrid", @class = "Grid" },
columns: webGrid.Columns(
webGrid.Column("CustomerId", "Customer Id"),
webGrid.Column("Name", "Name"),
webGrid.Column("Country", "Country")))
</body>
</html>
Screenshot
Errors
Following are the two errors encountered to me while creating the sample for this article. Thus for reference I have given their solutions below.
1. System.Data.StrongTypingException: The value for column 'IsPrimaryKey' in table 'TableDetails' is DBNull. System.InvalidCastException: Specified cast is not valid.
2. Unrecognized element 'providers'
Downloads