Getting started with ASP.NET Core and MySQL Connector/NET

This tutorial shows how to read information from MySQL Sakila database in an ASP.NET Core application using Connector/NET Core to explore the possibility to run this application on any .NET Core supported environment as Windows, Linux, and Mac.

Pre-requisites:

Windows and Visual Studio 2015

The first step is to create an ASP.NET Core Web Application (.NET Core) type project and name it MvcSakilaCore

Note: the project name should not contain spaces

 

cnet_aspnetcore_010

Select Web Application template and No Authentication:

cnet_aspnetcore_020

The new project will be created:

cnet_aspnetcore_030

Press F5 to run the application:

cnet_aspnetcore_040

Stop the running application to add some code.

Creating the data model

Create a new folder named “Models” where it will store the database access:

cnet_aspnetcore_050

Installing MySQL Connector/NET Core package

In order to use MySQL Connector/NET it’s required to add its nuget package.

In Solution Explorer, right-click MvcSakilaCore project > Manage NuGet Packages…
In the NuGet dialog, Browse “MySql.Data” package using version 6.10.0 or above:

cnet_aspnetcore_060

Adding the connection string

Add your connection string in the appsettings.json file:

cnet_aspnetcore_070

Adding data model classes

For this example a Film class will be used. It contains the database fields as properties we want to show in our application.

Add a new class named “Film” inside Models folder:

Create a new SakilaContext class that will contains the connections and Sakila database entities:

In order to be able to use our SakilaContext it’s required to register the instance as a service in our application. To do this add the code line in the Startup.cs file:

cnet_aspnetcore_090

Adding Film Controller

In Solution Explorer, right-click Controllers > Add > New Item… > MVC Controller Class
Name the controller FilmsController:

cnet_aspnetcore_100

cnet_aspnetcore_110

Change the FilmsController code to this:

Creating the Films View

Start creating the Films folder under Views:

In Solution Explorer, right click Views > Films > Add > New Item… > ASP.NET > MVC View Page

cnet_aspnetcore_130

Add the following code into the new Index.cshtml view file:

Before run the application, add the Films path to the running url.

In Solution Explorer, right click MvcSakilaCore > Properties > Debug > Launch URL > Films:

cnet_aspnetcore_150

Run the application (press F5) and the Films list should be displayed:

cnet_aspnetcore_160

 

MySQL Connector/NET 6.7.8, 6.8.6, and 6.9.7 have been released

Dear MySQL users,

MySQL Connector/Net 6.7.8, 6.8.6, and 6.9.7 are maintenance releases for the series of the .NET driver for MySQL. They can be used for production environments.

They are appropriate for use with MySQL server versions 5.5-5.7.

They are now available in source and binary form from http://dev.mysql.com/downloads/connector/net/
(note that not all mirror sites may be up to date at this point-if you can’t find this version on some mirror, please try again later or choose another download site.)

Changes in MySQL Connector/Net 6.8.8

  • Connections to MySQL server 5.7 now default to using SSL.

Changes in MySQL Connector/Net 6.8.6

  • Connections to MySQL server 5.7 now default to using SSL.

Changes in MySQL Connector/Net 6.9.7

  • The selection of a master or slave now takes into account
    both the status and mode, when before it only used the
    mode. Ignoring the status was problematic as, for
    example, an unreachable server’s status is marked as
    FAULTY while the mode does not change. (Bug #21203824)
  • Using MySqlConnection.Open() with Connector/Net 6.9.6
    would fail and raise the error “Unable to connect to
    Fabric server”. (Bug #20983968)
  • Connections to MySQL server 5.7 now default to using SSL.

The documentation is available at:
http://dev.mysql.com/doc/connector-net/en/

Nuget packages are available at:
https://www.nuget.org/packages/MySql.Data/
https://www.nuget.org/packages/MySql.Data.Entity/
https://www.nuget.org/packages/MySql.Fabric/
https://www.nuget.org/packages/MySql.Web/

Enjoy and thanks for the support!

On behalf of the MySQL Connector/NET Team.