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.
A connection between your C# application and a MySQL or MariaDB database is essential for any data-driven solution on the .NET platform. Whether you're creating a web app, desktop software, or backend service, a reliable database connection serves as the starting point for executing queries, handling data, and supporting dynamic functionality.
In this guide, you'll learn how to create a stable and efficient link between your .NET app and MySQL/MariaDB using dotConnect for MySQL, Devart's high-performance ADO.NET provider. We'll cover the full setup process—from initializing your project and installing the required NuGet package to configuring secure access with SSL/TLS. You'll also discover how to integrate seamlessly with modern ORM tools like Entity Framework Core using Scaffold-DbContext.
Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.
Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.
Offers many MySQL-specific features and fully supports all unique data types for accurate and complete data representation.
Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.
Features native integration with Visual Studio and complete design-time support for accelerated development.
Includes priority support, detailed documentation, and regular updates for continuous improvement.
You can start using dotConnect for MySQL immediately with a 30-day free trial. Choose one of the following installation options:
| Property | Description |
|---|---|
| Database | Default database to be used after connecting. |
| Host | Hostname or IP address of the MySQL server. |
| License Key | Your license key. This is required only when using .NET Standard compatible assemblies. |
| Password | Password for the user ID. |
| Port | Port number on which the MySQL server is listening. |
| UserId | User ID used to authenticate with MySQL. |
This example demonstrates a basic connection to a MySQL database. Depending on your requirements, you might want to add more functionality, such as executing queries or handling different types of exceptions.
static void Main(string[] args) {
string connectionString = "" +
"Host=127.0.0.1;" +
"User Id=TestUser;" +
"Password=TestPassword;" +
"Database=TestDb;" +
"License Key=**********";
try {
using (MySqlConnection connection = new MySqlConnection(connectionString)) {
connection.Open();
Console.WriteLine("Connection successful!");
}
}
catch (Exception ex) {
Console.WriteLine("An error occurred: " + ex.Message);
}
}
The connection string contains the necessary information to connect to the database, such as the host, user ID, password, and database name.
To establish the SSL connection to the MySQL database with the specified connection string parameters, modify your code as follows:
static void Main(string[] args) {
string connectionString = "" +
"Host=127.0.0.1;" +
"User Id=TestUser;" +
"Password=TestPassword;" +
"Database=TestDb;" +
"Protocol=SSL;" +
"SSL CA Cert=file://C:\\CA-cert.pem;" +
"SSL Cert=file://C:\\SSL-client-cert.pem;" +
"SSL Key=file://C:\\SSL-client-key.pem;" +
"License Key=**********";
try {
using (MySqlConnection connection = new MySqlConnection(connectionString)) {
connection.Open();
Console.WriteLine("SSL/TLS Connection successful!");
}
}
catch (Exception ex) {
Console.WriteLine("An error occurred: " + ex.Message);
}
}
You can also connect to the database using Visual Studio's built-in Data Explorer:
1. Go to the Server Explorer window, right-click Data Connections, and then select Add Connection.
2. In the Add Connection dialog, click Change to specify the data source.
3. In the Change Data Source dialog, select dotConnect for MySQL from the list of installed data providers and click OK.
4. In the Add Connection dialog, select MySQL as the data source and provide your server credentials.
5. To verify the connection, click Test Connection.
Once connected, you can use Data Explorer to browse tables, run queries, and manage data directly within Visual Studio.
Quickly build your EF Core model from your MySQL database using the visual Entity Developer wizard.
For a complete guide, see the full step-by-step instructions.
All steps in this guide are performed in Visual Studio, but 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.
To create a simple example that checks a database connection using Entity Framework Core (EF Core) with MySQL, follow these steps.
class Program {
static void Main(string[] args) {
// Assuming the SakilaContext class is generated by Entity Developer
using (var context = new SakilaContext()) {
try {
context.Database.CanConnect();
Console.WriteLine("Connection successful!");
}
catch (Exception ex) {
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
}
To connect to the MySQL database using Entity Framework Core (EF Core) and scaffold its context, follow this step-by-step instruction. This process generates the necessary EF Core model classes and a DbContext based on the existing MySQL database schema.
Install the required NuGet packages
In the Solution Explorer, right-click the project and select Manage NuGet Packages. Then, open the Package Manager Console. To do this, go to Tools > NuGet Package Manager > Package Manager Console
Scaffold the DbContext
In the Package Manager Console, run the following command to scaffold the DbContext and entity classes from the MySQL database. Replace the connection string with your relevant connection details.
Scaffold-DbContext "Host=127.0.0.1;Port=3306;User Id=TestUser;Password=TestPassword;Database=sakila;License Key=**********;" Devart.Data.MySql.EFCore -OutputDir Models
-OutputDir Models specifies the output directory where the generated model classes will be placed.
After running the scaffold command, EF Core generates the DbContext class and entity classes in the specified output directory (e.g., Models). Review the generated code to ensure it matches your database schema.
This article provides detailed guides on connecting a .NET application to MySQL or MariaDB databases in several ways. It explores various connection methods, including secure SSL/TLS connections and using Entity Framework Core with
Scaffold-DbContext.
When developing applications, you can make the most of Devart dotConnect for MySQL's solutions to achieve a much faster, smoother, and more straightforward performance of all tasks. Download a free trial to experience seamless database connectivity, performance optimizations, and robust security for your .NET projects!
License Key parameter for a working connection.
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.
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.
Scaffold-DbContext with a dotConnect connection string (including License Key) to generate the DbContext and entity classes.
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.