DEV Community

Deepangshi S.
Deepangshi S.

Posted on

Entity Framework Core | Object Relation Mapping | C# | A Beginner's Guide : Mastering

Entity Framework Core (EF Core) is a powerful Object-Relational Mapper (ORM) that simplifies data access in .NET applications. However, without proper optimization, your database queries can become slow and inefficient, impacting overall application performance.

Approaches in Entity Framework 💭

Database-First Approach
In a database-first approach to software development, the database schema is designed and created first. This initial database structure serves as the foundation for the application's data layer. Subsequently, the application's code, including models, controllers, and views, is generated or manually written to interact with this predefined database.

When to Use Database-First: 🤔

  • Legacy Systems: When working with existing databases, the database-first approach is often the most practical choice.
  • Team Collaboration: In teams where database designers and developers work independently, a database-first approach can help maintain consistency and alignment.
  • Data-Centric Applications: For applications where data integrity and consistency are paramount, a database-first approach can be beneficial.

Code-First Approach
In a code-first approach, the development process begins with writing the application code, including the data models. The database schema is then generated based on these models. This approach offers greater flexibility and agility, as changes to the application code can be directly reflected in the database schema.

Model-First Approach
A model-first approach involves creating a conceptual data model, often using a visual modeling tool like Entity-Relationship diagrams (ERDs). This model serves as the blueprint for both the database schema and the application code. It provides a high-level view of the data and its relationships, making it easier to understand and maintain.

Hybrid Approaches
Many organizations combine elements of these approaches to suit their specific needs. For instance, they might start with a high-level model-first design and then use a code-first approach to implement the details.

Key Considerations for Choosing an Approach:

  • Team Expertise: The skills and preferences of your development team can influence the choice of approach.
  • Project Complexity: For complex projects with many data relationships, a model-first approach can be beneficial.
  • Agile Development: Code-first and model-first approaches are often more suitable for agile development methodologies.
  • Legacy Systems: In cases where you're working with existing databases, a database-first approach might be necessary.
  • Data Integrity and Consistency: A model-first approach can help ensure data integrity and consistency across the entire system.
Tracking vs No-Tracking Queries in EF Core

EF Core supports two types of queries: tracking and no-tracking. The key difference lies in how EF Core manages changes to the queried entities.

Tracking Queries

EF Core keeps track of the changes to entities returned by a query.
Any updates to these entities are automatically detected and saved to the database during SaveChanges().

using (var context = new AppDbContext())
{
    var product = context.Products.FirstOrDefault(p => p.Id == 1); 
    product.Name = "Updated Product";  
    context.SaveChanges();            
}
Enter fullscreen mode Exit fullscreen mode
No-Tracking Queries
  • EF Core does not track the changes to entities returned by the query.
  • This is faster and uses less memory, making it ideal for read-only scenarios.
using (var context = new AppDbContext())
{
    var product = context.Products.AsNoTracking()
                     .FirstOrDefault(p => p.Id == 1); 
    product.Name = "Updated Product";  
    context.SaveChanges();             
}
Enter fullscreen mode Exit fullscreen mode
When to Use 🤔
  • Tracking Queries: Use when you need to modify and save entity changes.
  • No-Tracking Queries: Use for read-only operations to improve performance.

Top comments (0)