Entity Framework Core (EF Core) is an object relational mapper provided by .NET.
It allows you to map C# objects to relational database objects including Microsoft SQL and PostgreSQL, among others.
We will explore the benefits of using EF core as opposed to using direct SQL queries or ADO.NET connections.
Productivity
- EF Core removes the need to write any hard-coded SQL queries as all queries can be written using LINQ, which also reduces the learning curve for developers who are not familiar with SQL queries.
- It automatically handles data type conversions between models and database objects.
Maintainability & Security
- Making changes is easier and does not introduce the risk of breaking changes as database objects are strongly typed. Changes to the database are managed by migrations instead of hard-coded SQL.
- Reduced risk of SQL injection attacks as SQL queries executed on the database are managed by EF Core.
Cross-platform capability
- EF Core is written in the .NET Core framework so it lends itself to cross-platform usage, it can be used in a Linux environment as well as a Windows environment
- EF Core also supports multiple different databases, including MS SQL, Postgresql, MySql, SQLite. Which gives the ability to switch between databases with not much code changes.
Performance
- EF Core using LINQ allows Eager Loading and Lazy Loading which reduces loading data prematurely or unnecessarily loading data.
- EF Core automatically handles change tracking of entities in the database so not many code changes are needed to keep the database in sync.
EF Core In Action
Let’s look at a sample C# class that was used to update a table in a PostgreSql database using a migration. We have a UserProfile model and we want to add an age field to this table.
DbContext
public class WeatherDbContext : DbContext
{
public DbSet<WeatherLog> WeatherLogs { get; set; }
public DbSet<UserPreference> UserPreferences { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
public WeatherDbContext(DbContextOptions<WeatherDbContext> options)
: base(options) { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
}
Model before change (with added Age field)
public class UserProfile
{
[Key]
public int Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Age { get; set;}
}
In the terminal or Package Manager Console, navigate to the path where your csproj file is located and run the following command (name can be whatever you want):
dotnet ef migrations add AddAgeFieldToUserProfile
Once that has run, it will create a migration which will look like this:
public partial class AddAgeToUserProfiles : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "Age",
table: "UserProfiles",
type: "int",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Age",
table: "UserProfiles");
}
}
If you want to edit the migration and add additional queries or changes to the database you can edit the migration, or you can run the following command to execute the migration on the database:
dotnet ef database update
If for any reason you need to rollback you can run the following command:
If there are no previous migrations:
dotnet ef database update 0
If you want to rollback to a specific migration:
dotnet ef database update {nameOfMigrationToRollbackTo}
By implementing EF Core in your application you increase the maintainability, security and performance of your database interactions.