HowTo: Using MySQL for Visual Studio in you first ASP.NET MVC Application with EF 6
By Gab Martinez-Oracle on Jul 25, 2014
- File menu -> New -> Click on MySQL category and select MySQL Script.
- Connect to your MySQL Server instance so the script can be executed to create the database.
- 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.
- Execute the sakila-data.sql the same way that the previous step.
- 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.
- 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.
- 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.
- 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.
- 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.
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.
Hope you found this information useful.
Happy MySQL/.Net coding!