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.
Why dotConnect for MySQL?
Advanced ORM support
Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other technologies for efficient data management.
Full ADO.NET compliance
Conforms to the latest ADO.NET standards and innovations for seamless integration with .NET applications.
MySQL-specific data types
Offers many MySQL-specific features and fully supports all unique data types for accurate and complete data representation.
Secure connection options
Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.
IDE integration
Features native integration with Visual Studio and complete design-time support for accelerated development.
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:
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.
Connect using C#
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.
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.
classProgram {
staticvoidMain(string[] args) {
// Assuming the SakilaContext class is generated by Entity Developerusing (var context = new SakilaContext()) {
try {
context.Database.CanConnect();
Console.WriteLine("Connection successful!");
}
catch (Exception ex) {
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}
}
Connect with EF Core using Scaffold-DbContext
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.
The -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.
Video tutorial: How to connect .NET console application to a MySQL database
Conclusion
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!
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 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.
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.