# Connect to MySql in .NET and Entity Framework
[**Article Source**](https://gitlab.com/Syndamia/syndamiadotcom/-/blob/master/src/tutorials/connect-to-mysql-in-dotnet-ef/article.md) || [**GitLab Discussion**](https://gitlab.com/Syndamia/syndamiadotcom/-/issues/3)
**Created: 21 May 2020** || **Edited: 19 June 2021**
---
I have found that connecting a `.NET` application to a `MySql` database (with Entity Framework) can be tricky. Here is what works for me.
Also, [MySql.Data.EntityFrameworkCore](https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore/) has been **depricated** since *October 2020*. If you need setup instructions with it, read [this](https://gitlab.com/Syndamia/syndamiadotcom/-/blob/0510e29526c6d7681587fff4c98a339e8c69e9b5/src/tutorials/connect-to-mysql-in-csharp-ef/article.md).
## Contents
.UseMySql(CONNECTION_STRING, new MySqlServerVersion(new System.Version(8, 0, 21)))
Where `CONNECTION_STRING` is a (constant) variable that contains your connection string, and 8, 0, 21 are the version numbers.
### ASP.NET Startup
[](#ASP.NET%20Startup)
For `ASP.NET` applications, the connection must be put inside the `ConfigureServices` method of the `Startup.cs` file. If you already have a `.UseSQL` method call in there, just replace it with your `.UseMySql` method and arguments.
The result should look something along the lines of:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<AppDbContext>(opt =>
{
opt.UseMySql(...);
});
}
### DBContext child class
[](#DBContext%20child%20class)
Another way to connect is via a `DBContext` child class (so, inside your own DB context). Just, override the `OnConfiguring` method and call the `.UseMySql` method from the `optionsBuilder` parameter.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql(...);
}