HowTo: Using MySQL for Visual Studio in you first ASP.NET MVC Application with EF 6

Last week it was released the RC version of MySQL for Visual Studio 1.2.2. In this blog post we'll be showing one of the new features in this release. And we will be doing a short demo about some of the first steps when starting to use MySQL with .NET applications.

A very frequent comment or question we have from the users is about where to begin when they are just starting to use MySQL with the Visual Studio IDE. This post is intended to give an introduction to one of the new features of MySQL for Visual Studio 1.2.2 RC that will help in the process when you want to develop ASP.NET MVC applications with MySQL and a data access technology like Entity Framework. 

Requirements
  • Visual Studio 2013 Professional edition or higher.
  • MySQL Connector/Net 6.9.x driver available on the MySQL Developer Zone
  • MySQL for Visual Studio 1.2.2 available on the MySQL Developer Zone
  • A MySQL server instance running. It can be use 5.5.x, 5.6.x or 5.7.x

There is also this blog post that it is a good reading about what tools are needed in order to start developing applications with MySQL and .NET using Visual Studio.

For this demo it will be used the Sakila database. A script to create the database and add test data is available here: http://dev.mysql.com/doc/index-other.html

Once you have downloaded the scripts, these can be run inside Visual Studio by using the MySQL Script editor. Just follow these steps:

  1. File menu -> New -> Click on MySQL category and select MySQL Script.

  2. Connect to your MySQL Server instance so the script can be executed to create the database.

  3. The first script to run is the sakila-schema.sql. Paste the script into this window editor and run it by clicking on the Run SQL button from the toolbar of the editor. 
  4. Execute the sakila-data.sql the same way that the previous step.

The second part of this demo is for creating the web application. For that you'll have to open the MySQL Tool bar in order to access the New MySQL project dialog.

If you just installed the MySQL for Visual Studio product, then the MySQL Toolbar has to be selected on View menu -> toolbars -> MySQL so it can be shown.

On the New MySQL Project dialog select the MySQL ASP.NET MVC 3 Project with the language you prefer

If you're using Visual Studio 2013 this wizard will change the MVC version to the MVC 4 version.

The MySQL ASP.NET MVC wizard helps on the creation of a new MVC application so this can be done in just a couple of minutes so you can start right away playing with MVC and Entity Framework. The wizard will guide you through the steps. As they will be described here:

  1. Setup the connection to be used in the web application. For this demo we will use the sakila database. Since this is development environment this server instance does not have a password for the connection which is not recommended in a production environment. You can choose between adding or not the credentials in the connection string that will be added to the web.config file. Please notice that the authentication is included in this application and also the Role and Profile providers are added optionally in this step. All of this is handled in the MySQL database that is selected.
  2. Setup the minimum password length for the user registration in the application. And some others settings like require password question and answer, write to event log and creating the first user for the website authentication.

  3. Set up the connection for the database that will be used for the Entity Framework Data model. This database should have at least a table with a primary key. So it can be added to the Data Entity Model. You can choose between the 5.0 version of EF or the 6.0. A nuget package will be added in order to add the EF libraries. The MySQL .NET driver will be added too. The Data entity model is an optional part of the demo application. If you don't add any model then there won't be any View in the Application to be generated. Only the user registration view and login page are always added.

  4. Select the tables to include in the MVC application for the generated views. You can select as many tables in the database are. Each table will be used to generate a new view. 
  5. Click on finish.

At this moment the wizard will create the MVC application for you. This application will have a model, if selected, and all the views and controllers for listing the records contained in the database. You do not need to configure or setup anything else. Just click on Run to see the application working.

The following screenshots show the application generated.

Solution explorer window showing all the items of the resulting project.


Home page view of the web application generated.


Screenshot of the view for the address list table.


Conclusion

This is a quick demo that shows how to create a new ASP.NET MVC application from scratch. Just having a MySQL database. The result is an MVC ASP.NET application that uses MySQL Membership, roles and profiles providers and an Entity Framework model that works with the MVC items in the application. All of this with just a few clicks and no coding needed.

Quick links

- MySQL for Visual Studio download page

- MySQL for Visual Studio documentation 

- MySQL Community Bugs

- MySQL for Visual Studio Forum

Hope you found this information useful.

Happy MySQL/.Net coding! 


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Here's where you'll hear about all the new and fun stuff we are doing with MySQL on Windows.

Search

Categories
Archives
« September 2015
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today