This tutorial shows how to connect a C# application to MySQL and MariaDB using Entity Framework Core with dotConnect for MySQL. You’ll create EF Core models using Entity Developer—a visual ORM builder and Scaffold-DbContext, build a console app, and perform CRUD operations through a structured database access layer.

Why dotConnect for MySQL?

ORM support provided

Advanced ORM support

Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.

Full ADO.NET compliance

Full ADO.NET compliance

Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.

Support for MySQL-specific data types

MySQL-specific data types

Offers many MySQL-specific features and fully supports all unique data types for accurate and complete data representation.

Secure connection ensured

Secure connection options

Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.

Integration with popular IDEs

IDE integration

Features native integration with Visual Studio and complete design-time support for accelerated development.

Priority support provided

Priority support & frequent updates

Includes priority support, detailed documentation, and regular updates for continuous improvement.

Download and activate dotConnect for MySQL

You can start using dotConnect for MySQL immediately with a 30-day free trial. Choose one of the following installation options:

30-day free trial version

dotnet add package Devart.Data.MySql
Install-Package Devart.Data.MySql

You can install the driver by using the Windows installer.

After you receive the license key, add it to your connection strings to connect to the data source.

Start using dotConnect for MySQL in your project today with a free trial

Create an EF Core model

Once you have configured the database, you can move on to the next step: creating an EF Core model. You can do this in two ways: using Scaffold-DbContext or via Entity Developer.

Create an EF Core model via Entity Developer

Entity Developer allows you to visually design and generate EF Core models, making database application development faster, easier, and more efficient. If you don't have it already installed, close your Visual Studio instance, download Entity Developer, and install it following the on-screen instructions.

Follow the detailed illustrated guide to create your database model using Entity Developer. When this process is complete, the model you created opens, so you can work with it.

Finished model

This guideline was designed with Visual Studio in mind. However, if you prefer VS Code, JetBrains Rider, or another similar tool, you can use ED as a standalone application to work with the models efficiently.

Create an EF Core model using Scaffold-DbContext

You can use Scaffold-DbContext to generate DbContext and entity classes for your database. Run the following command, replacing values with your actual credentials:

After you execute this command, the ModelContext file and the Models folder containing the table entity classes get generated.

Scaffold-DbContext "Host=127.0.0.1;Port=3306;User Id=TestUser;Password=TestPassword;Database=sakila;License Key=**********;" Devart.Data.MySql.EFCore -OutputDir Models

Connection strings

Property Meaning
Host Hostname or IP address of the MySQL server
Port Port number on which the MySQL server is listening
User Id User ID used to authenticate with MySQL
Password Password for the user ID
Database Default database to be used after connecting
License key Your license key (only required when using .NET Standard compatible assemblies)

Connect to MySQL and retrieve data

This section demonstrates how to connect to a MySQL database and retrieve the first 10 rows from the Actor table using EF Core. The SakilaModel DbContext is configured with a MySQL connection string in its OnConfiguring method. We will query the data and display it in the console for verification.

using MySQL_EF_Core;

namespace MySql_EF_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var context = new sakilaModel())
                {
                    var actors = context.Actors
                        .Take(10)
                        .ToList();

                    Console.WriteLine("First 10 Actors:");
                    Console.WriteLine("---------------");
                    foreach (var actor in actors)
                    {
                        Console.WriteLine($"ID: {actor.ActorId}, Name: {actor.FirstName} {actor.LastName}, Last Update: {actor.LastUpdate:yyyy-MM-dd HH:mm:ss}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}

Run the application. If everything is configured correctly, the console will display the first 10 records from the Actor table.

Connect to MySQL and retrieve data


Insert new records using EF Core

In this section, we insert a new test actor into the Actor table. EF Core's Add method stages the record, while SaveChanges persists it to the database.

using MySQL_EF_Core;

namespace MySql_EF_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var context = new sakilaModel())
                {
                    var newActor = new Actor
                    {
                        FirstName = "Test",
                        LastName = "Actor",
                        LastUpdate = DateTime.Now
                    };

                    context.Actors.Add(newActor);
                    int rowsAffected = context.SaveChanges();

                    Console.WriteLine("Insert Result:");
                    Console.WriteLine("-------------");
                    Console.WriteLine($"Rows affected: {rowsAffected}");
                    Console.WriteLine($"Inserted Actor - ID: {newActor.ActorId}, Name: {newActor.FirstName} {newActor.LastName}, Last Update: {newActor.LastUpdate:yyyy-MM-dd HH:mm:ss}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}

Run the application. The console will display the inserted record for verification purposes.

Insert new records using EF Core

Update MySQL data using EF Core

This section demonstrates how to update the test actor's first and last names. We retrieve the record by first name and last name, modify the properties, and call SaveChanges to store the modifications.

using MySQL_EF_Core;

namespace MySql_EF_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var context = new sakilaModel())
                {
                    var actor = context.Actors
                        .FirstOrDefault(a => a.FirstName == "Test" && a.LastName == "Actor");

                    if (actor != null)
                    {
                        actor.FirstName = "Updated";
                        actor.LastName = "Star";
                        actor.LastUpdate = DateTime.Now;
                        int rowsAffected = context.SaveChanges();

                        Console.WriteLine("Update Result:");
                        Console.WriteLine("-------------");
                        Console.WriteLine($"Rows affected: {rowsAffected}");
                        Console.WriteLine($"Updated Actor - ID: {actor.ActorId}, Name: {actor.FirstName} {actor.LastName}, Last Update: {actor.LastUpdate:yyyy-MM-dd HH:mm:ss}");
                    }
                    else
                    {
                        Console.WriteLine("Test Actor not found.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}

After running the application, the console displays the updated record for verification.

Update MySQL data using EF Core

Delete MySQL data using EF Core

Finally, we delete the test actor from the Actor table. The record is retrieved by first name and last name, removed using the Remove method, and SaveChanges commits the deletion.

using MySQL_EF_Core;

namespace MySql_EF_Core
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var context = new sakilaModel())
                {
                    var actor = context.Actors
                        .FirstOrDefault(a => a.FirstName == "Updated" && a.LastName == "Star");

                    if (actor != null)
                    {
                        context.Actors.Remove(actor);
                        int rowsAffected = context.SaveChanges();

                        Console.WriteLine("Delete Result:");
                        Console.WriteLine("-------------");
                        Console.WriteLine($"Rows affected: {rowsAffected}");
                        Console.WriteLine($"Deleted Actor - ID: {actor.ActorId}, Name: {actor.FirstName} {actor.LastName}");
                    }
                    else
                    {
                        Console.WriteLine("Test Actor not found.");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}

After running the application, the console confirms the successful deletion.

Delete MySQL data using EF Core

Video tutorial: How to Connect EF Core to MySQL and MariaDB

Conclusion

In this tutorial, you learned how to integrate MySQL with Entity Framework Core using dotConnect for MySQL. This combination simplifies data access and enables clean, scalable development for .NET applications. With dotConnect's advanced capabilities and EF Core's familiar workflow, you are equipped to build robust solutions backed by MySQL.

FAQ

How do you install and activate dotConnect for MySQL in a .NET project?
Install dotConnect for MySQL via the EXE installer or by adding the Devart.Data.MySql NuGet package to your project, then obtain your personal activation key from your Devart Customer Portal and include it in the connection string via the License key parameter for a working connection.
How do you create a connection to MySQL using dotConnect in C#?
Define a connection string that includes host, user ID, password, database, and the License key value, then create a MySqlConnection instance with this string and call Open() for it inside a try-catch block to test and handle connection errors.
How do you enable SSL/TLS for secure MySQL connections with dotConnect?
Add Protocol=SSL and specify the SSL CA Cert, SSL Cert, and SSL Key file paths in the connection string, then open the MySqlConnection connection to establish an encrypted SSL/TLS connection.
Can you connect to MySQL using Entity Framework Core and dotConnect?
Yes, you can either use Entity Developer to visually create an EF Core model from the database or run Scaffold-DbContext with a dotConnect connection string (including the License key) to generate the DbContext and entity classes.
Is it possible to connect to MySQL using Visual Studio Server Explorer with dotConnect?
Yes, in Visual Studio Server Explorer, you add a new Data Connection, choose dotConnect for MySQL as the data provider, enter your MySQL server credentials, test the connection, and then browse and manage data directly from the IDE.

Dereck Mushingairi

I'm a technical content writer who loves turning complex topics — think SQL, connectors, and backend chaos–into content that actually makes sense (and maybe even makes you smile). I write for devs, data folks, and curious minds who want less fluff and more clarity. When I'm not wrangling words, you'll find me dancing salsa, or hopping between cities.

dotConnect for MySQL

Get an enhanced ORM-enabled data provider for MySQL and develop .NET applications working with MySQL data quickly and easily!

Try the 30-day trial of the full product. No limits. No card required. Start free trial