MySQL ADO.NET Developer Guide - Entity Framework ASP.NET App
This article provides a walkthrough for using Entity Framework (EF) Core in an ASP.NET Core Razor Pages app. With the help of the CData ADO.NET Provider for MySQL, this application will create pages to view, add, delete, and update data from MySQL
NOTE: While this article refers to MySQL data, the principles can be applied to any of the hundreds of data sources CData supports.
Prerequisites
This guide requires the following parameters:
- Visual Studio 2022
- .NET 8.0
- CData ADO.NET Provider for MySQL
- Sample MySQL Database
Guide
-
Create the application
-
Open Visual Studio and create a new “ASP.NET Core Web App (Razor Pages)” project.
-
Name your project “WebAppMySQL”, so that the namespaces will match if you copy and paste code from this guide, and then select “Next”.
-
On the next page, select “.NET 8.0 (Long-term support)” and then click “Create”.
-
Open Visual Studio and create a new “ASP.NET Core Web App (Razor Pages)” project.
-
Install Entity Framework and add references to the required assemblies
-
Use the NuGet Package Manager to add the packages listed below to your project:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
- Add a reference to System.Data.CData.MySQL.dll, located in the lib -> net8.0 subfolder in the installation directory (ex. "C:\Program Files\CData\CData ADO.NET Provider for MySQL 2024\").
- Add a reference to CData.EntityFrameworkCore.MySQL.dll, located in the lib -> net8.0 -> EFCORE80 subfolder in the installation directory.
- Add the included licensing file (.lic) to your project. You can do this by right-clicking the project in the Solution Explorer and then navigating to Add -> Existing Item -> System.Data.CData.MySQL.lic (located in the lib -> netstandard2.0 subfolder in your installation directory). Finally, right-click the licensing file, and set the Copy to Output Directory property to Copy if newer.
-
Build the project to complete the setup for using EF Core.
-
Use the NuGet Package Manager to add the packages listed below to your project:
-
Reverse Engineering (Scaffolding) the data model
Scaffolding is performed using the Package Manager Console (PMC), so open the PMC and use the following command to scaffold the schema into your Models folder. This command automatically constructs classes for all tables/views available. It also creates a context class that extends DbContext and exposes the DbSet properties that represent the tables in the data source:
Scaffold-DbContext "Authscheme=Password;Server=YourServer;Port=YourPort;User=YourUser;Password=YourPassword;Database=YourDatabase;Tables=customers;" CData.EntityFrameworkCore.MySQL -OutputDir Models -Context MySQLContextNote: We set the Tables property to restrict the number of tables we connect to and model. This will only model the Customer table from the MySQL connection instead of every available table and view.
Read more about the CData MySQL ADO.NET Provider connection properties on the CData Documentation page: CData ADO.NET Provider for MySQL Documentation
-
Add a Razor Page for the Customers
- Create a new "Customers" subfolder in the “Pages” folder of your project.
- In the Solution Explorer, right-click the new “Customers” folder and select “Add” > “Razor Page…”.
- In the Razor Page dialog, select “Razor Page using Entity Framework (CRUD)” and click "Add".
-
In the “Razor Page using Entity Framework (CRUD)” dialog:
- Set “Model class” to the Customer entity, "Customer"
- Set “DbContext class” to the database context, “MySQLContext”.
-
Click “Add” to create the new pages. This will create Razor pages in the Pages/Customers folder:
-
Add Connection String in the appsettings.json file
In the appsettings.json file, you will need to add the connection string for the MySQL connection. Below is an example of the code:
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": {"MySQLContext": "Authscheme=Password;Server=YourServer;Port=YourPort;User=YourUser;Password=YourPassword;Database=YourDatabase;Tables=customers;" } } -
Register the context in Program.cs
In the Program.cs file, you will need to register the context class with the dependency injection container. Below is an example of the code:
using WebAppMySQL.Models; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddDbContext<MySQLContext>(options => options.UseMySQL(builder.Configuration.GetConnectionString("MySQLContext"))); -
Adjust the layout of the Customers list
For ease of use, we will adjust the layout of the Customers page so that the controls to Edit or Delete a Customer are easily accessible. By default, the column with these controls is listed last, so we will adjust the code in the Index.cshtml file to make this column the first one displayed. In this code, we added an empty header for the column and moved the Edit and Delete controls to the first column listed. Below is an example of the adjustments made:
Formatter [ "Title" ] failed in the evaluation of @page @model WebAppMySQL.Pages.Customer.IndexModel @{ ViewData\\["Title"] = "Index"; }
Index
. The error was: Attempt to reference the value of an attribute using an invalid name "Title" @foreach (var item in Model.Customer) { � � }Edit | Delete � �