MySQL for Visual Studio  /  MySQL Project Items  /  MySQL ASP.NET MVC Items

8.1 MySQL ASP.NET MVC Items

To add a MySQL MVC Item to an existing MVC project, first add a MySQL Entity Framework model. Skip this step if you have already done this.

Configure the project to use MySQL with an Entity Framework. There are two ways to do this:

  • Manually add the references needed (EntityFramework, MySql.Data &MySql.Data.Entity), and add the required configuration to the web.config configuration file

  • Or (preferred), take advantage of the MySQL Website Configuration tool, which allows either Entity Framework 5 or 6 with MySQL. For additional information about this tool, see Chapter 7, MySQL Application Configuration Tool.

Once you have configured the project to use MySQL with Entity Framework, proceed to create the model using the standard ADO.NET Entity Data Model wizard. For MySQL MVC Item Templates, you need to add the model under the "Models" folder, as illustrated below:

Figure 8.1 ADO.NET Entity Data Model

ADO.NET Entity Data Model

Figure 8.2 Choose or create a new MySQL connection

Choose or create a new MySQL connection

Figure 8.3 Creating a new MySQL connection

Creating a new MySQL connection

After selecting the MySQL connection, you need to select the database objects to include in the model.

Important

The Pluralize or singularize generated object names option must remain unchecked, otherwise the MySQL MVC Item Template will not function properly.

Figure 8.4 Selecting the database object to include in the model

Selecting the database object to include in the model

Click Finish to generate the model, as demonstrated below:

Figure 8.5 Creating the MySQL Entity Framework model

Creating the MySQL Entity Framework model

Now, generate a new MySQL MVC Item. Right-click on the project, and select Add New Item from the contextual menu.

Figure 8.6 Add New Item

Add New Item

This launches the Add New Item wizard. The MySQL menu offers two options: MySQL New MVC Item and MySQL New Windows Form. Select MySQL New MVC Item, and then click Add.

Figure 8.7 The MySQL menu options

The MySQL menu options

This opens the MVC Item Template dialog. Now select the MySQL model and entity that you want to use to create the MVC item. The model dropdown list is populated based on all the MySQL Entity Framework models available in the project, entities dropdown list is populated with entities available for the selected model.

Figure 8.8 MySQL MVC Item Template Dialog

MySQL MVC Item Template Dialog

Figure 8.9 MySQL MVC Item Template

MySQL MVC Item Template

After selecting the model and entity to create the item, click Finish, and a new controller and view matching the selected entity will be added to the project. These contain the necessary back end code to render the entity data.

Figure 8.10 New controller and view added to the project

New controller and view added to the project

You can now execute the application. In our example we used the Sakila database and generated an Actor controller:

Figure 8.11 The Actor View

The Actor View