Connecting to a Database with EF Core

In previous posts, I mentioned that using Entity Framework is the same no matter which database you use; the only thing that changes is how you set up the connection.

 

Each database we are going to use has specific Nuget packages for each one of them.

In all cases, the information is added along with the instruction AddDbcontext for the dependency container.

 

 

1 - Connecting to MySQL with Entity Framework Core

MySQL is the database for this course. In fact, it's the one I usually use every day for my personal projects.

For this example, I'm going to hardcode the URL, but, of course, you can get it from the configuration using the options pattern.

 

For MySQL we need the MySql.EntityFrameworkCore package and just use the UseMySQL method:

services.AddDbContext<CursoEfContext>(options=>
    options.UseMySQL("server=127.0.0.1;port=4306;database=cursoEF;user=root;password=cursoEFpass"));

 

 

2 - Connecting to SQL Server with Entity Framework Core

One of the most used databases when working in .NET is Microsoft's own, Microsoft SQL Server. To connect, we need to install the Microsoft.EntityFrameworkCore.SqlServer package, and we only have to use the UseSqlServer method:

services.AddDbContext<CursoEfContext>(options=>
    options.UseSqlServer(configuration.GetConnectionString("SQLServerConnection")));

 

 

3 - Connecting to Postgre SQL with Entity Framework Core

After MySQL, PostgreSQL is possibly the most popular open source option out there. To connect, we install the Npgsql.EntityFrameworkCore.PostgreSQL package and use the UseNpgsql method:

services.AddDbContext<CursoEfContext>(options=>
    options.UseNpgsql(configuration.GetConnectionString("PostgreSQLConnection")));

 

 

4 - Connecting to SQLite with Entity Framework Core

Another very popular open source option, especially for small or hobby projects, is SQLite. To connect, we'll use the Microsoft.EntityFrameworkCore.Sqlite package and the UseSqlite method:

services.AddDbContext<CursoEfContext>(options=>
    options.UseSqlite(configuration.GetConnectionString("SqLiteConnection")));

 

 

5 - Connecting to Azure Cosmos DB with Entity Framework Core

Now we're in the cloud, specifically Azure and its CosmosDB database. To connect, we use the Microsoft.EntityFrameworkCore.Cosmos package and the UseCosmos method:

services.AddDbContext<CursoEfContext>(options=>
    options.UseCosmos(configuration.GetConnectionString("AzureCosmosConnection"), "dbName"));

Note: In AWS, both Aurora and RDS use other databases behind the scenes, so you have to use that connection. For example, if you use MySQL, you need to use `UseMySQL`, etc.

 

 

6 - Connecting to Oracle with Entity Framework Core

Finally, the database that's not dead, it's just out partying, Oracle. To connect, we'll use the Oracle.EntityFrameworkCore package and the UseOracle method:

services.AddDbContext<CursoEfContext>(options=>
    options.UseOracle(configuration.GetConnectionString("OracleConnection")));

 

 

Conclusion

In this post, we've seen how to connect to different databases very easily when using Entity Framework Core.

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café