What is Entity Framework Core?
Entity Framework (EF) Core stands as an Object-Relational Mapper (ORM) Framework tailored for data access within .NET Core. Rolled out in tandem with .NET Core, it represents an extensible, lightweight, open-source, and cross-platform rendition of the Entity Framework data access technology. Notably, EF Core boasts compatibility across various operating systems such as Windows, Mac, and Linux.
What is ORM?
ORM, short for Object-Relational Mapper, performs a dual role: it automatically crafts classes derived from database tables and vice versa. Additionally, it possesses the capability to generate the requisite SQL commands for database creation based on these classes.
For developers immersed in data-centric applications, the ORM Framework proves indispensable. It effortlessly generates SQL commands tailored for CRUD operations, ensuring compatibility with the underlying database. Essentially, the ORM Framework obviates the necessity for much of the manual data access code typically authored by developers.
Why Do We Need to Use An ORM Framework?
Using an ORM Framework, such as Entity Framework (.NET Framework) or EF Core (.NET Core), streamlines data management in applications. For instance, consider developing a student management system for a college. Without an ORM, extensive custom data access code would be necessary to perform CRUD operations on database tables. This entails writing SQL statements and mapping data to model classes manually, a repetitive task for developers. However, an ORM automates these processes, reducing development time significantly. Acting as an intermediary between application code and the database, an ORM eliminates the need for much of the custom data access code typically written without its integration.
Main Components
DbContext: Represents the database session and facilitates querying and interaction with the database.
DbSet: Signifies a collection of entities within the database.
Entity Classes: Plain Old CLR Objects (POCO) that correspond to database tables.
Configuration: Involves using Fluent API or Data Annotations to configure entity properties and relationships.
Migrations: Utilised for managing and applying database schema modifications.
LINQ (Language Integrated Query): Employed for querying data in a type-safe manner.
Implementation
Create Entities
Student.cs
public class Student { public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int GradeId { get; set; } public Grade Grade { get; set; } }
Grade.cs
public class Grade { public Grade() { Students = new List<Student>(); } public int GradeId { get; set; } public string GradeName { get; set; } public IList<Student> Students { get; set; } }
The Student class outlined above consists of properties associated with students, such as StudentId, FirstName, and LastName. Additionally, it includes GradeId and Grade properties, which are linked to the GradeId property of the Grade class. This linkage is necessary because each student is affiliated with a specific grade, and a grade can encompass multiple students.
These two basic domain classes represent our model, yet they have not been configured as entities within the Entity Framework framework. While “entities” and “domain classes” are frequently used interchangeably, they encompass slightly different concepts.
Entities within the Entity Framework are directly linked to tables in the database. The Entity Framework system maintains a record of these entities, enabling it to execute CRUD (Create, Read, Update, Delete) operations on the database automatically, depending on the status of their corresponding objects.
Domain classes, on the other hand, encapsulate the fundamental functionalities and business regulations of the application, guaranteeing the correct implementation of business logic.
The Student and Grade classes serve as domain classes. To utilize them as entities within the Entity Framework, they must be included as DbSet<T> properties within the DbContext class. This inclusion enables the Entity Framework engine to monitor their status effectively.
Creating DbContext class
In EF Core, the responsibility of DbContext class is to perform the following task:
- Manage database connection
- Configure model & relationship
- Querying database
- Saving data to the database
- Configure change tracking
- Caching
- Transaction management
SchoolContext.cs
public class SchoolContext : DbContext { //entities public DbSet<Student> Students { get; set; } public DbSet<Grade> Grades { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); } }
The DbSet<TEntity> type grants EF Core the capability to query and store instances of the designated entity within the database.
Subsequently, the optionsBuilder.UseSqlServer() is an extension method utilized to configure EF, enabling it to employ SQL Server as the database provider. This is achieved by specifying a database connection string.
Use case of EF Core
- Rapid Development and Productivity: EF Core’s high-level abstractions, like LINQ support and automatic change tracking, accelerate development. Moreover, opt for EF Core when prioritising application logic over SQL queries, enhancing productivity.
- Complex Data Models and Relationships: If your application boasts intricate data models with numerous entity relationships, EF Core’s navigation properties and automatic relationship handling streamline code complexity.
- Code-First Development: EF Core excels in code-first development paradigms, where C# classes define the data model, allowing for automatic generation of the database schema. This approach expedites initial development efforts.
- Tooling and Ecosystem: Benefit from EF Core’s extensive tooling ecosystem, featuring migrations for streamlined database schema management, Visual Studio integration, and comprehensive documentation. As a more mature and feature-rich ORM, EF Core offers robust support for development workflows.
Conclusion
To conclude, we learned about what functionality EF Core provides to a .NET Core application, how we can use it to set a direct connection to database and retrieve or update data from the database, different components of the EF Core commonly used and what use cases we can use it in.
