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.
Index
- 1 - Connecting to MySQL with Entity Framework Core
- 2 - Connecting to SQL Server with Entity Framework Core
- 3 - Connecting to Postgre SQL with Entity Framework Core
- 4 - Connecting to SQLite with Entity Framework Core
- 5 - Connecting to Azure Cosmos DB with Entity Framework Core
- 6 - Connecting to Oracle with Entity Framework Core
- Conclusion
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.
If there is any problem you can add a comment bellow or contact me in the website's contact form