Search This Blog

Monday, September 22, 2025

Q36-Q40(EF)

Q36. What is the difference between Include and Join in entity framework?
Q37. What will be your approach in case you want to call a manual stored procedure from your dot net code with Entity Framework?
Q38: How to optimize entity framework queries if taking time?
Q39: What are the various approach in EF. Tell the difference between them. 
Q40. What is lazy loading in entity framework.?

=============================================================================
Q36. What is the difference between Include and Join in entity framework?

Answer:
Refer -> .NET Core Q21

=============================================================================
Q37. What will be your approach in case you want to call a manual stored procedure from your dot net code with Entity Framework?

Answer:
Refer -> .NET Core Q25
=============================================================================
Q38: How to optimize entity framework queries if taking time?

Answer:

1. Ensure proper indexes exist for frequently queried columns at database side. 
2. Review the execution plan of the generated SQL to catch table scans or bad joins.
3. Avoid fetching unnecessary data: Use Select to project only required columns instead of pulling full entities.
4. Use AsNoTracking() for read-only queries to skip change tracking overhead.
5. Be mindful with Include – only eager load what’s needed. Overusing it can explode query size.
6. Use IQueryable and filter the data at database side. database side filtering. 

Snip 1:
var productNames = context.Products
    .Where(p => p.IsActive)
    .Select(p => p.Name)
    .ToList();

=============================================================================
Q39: What are the various approach in EF. Tell the difference between them. 

Answer:
There are 3 approaches we can proceed with Database First, Model First and Code First. 

Database-First
  • You start with an existing database.
  • EF generates entity classes and a model (.edmx in EF6, or scaffolding in EF Core) from the schema.
  • Any changes in the database require updating the model from the database.
  • Useful when the DB already exists and is the source of truth.
Model-First
  • You start by designing your conceptual model (entities, relationships) visually in the EF Designer (.edmx).
  • EF generates both the database schema and entity classes from the model.
  • Changes to the model flow down to the database via generated DDL scripts.
  • Good for greenfield projects where you want to think in terms of domain entities first, not database tables
Code-First
  • You start by writing C# classes (POCOs – Plain Old CLR Objects).
  • EF builds the database schema based on these classes and configuration (attributes or Fluent API).
  • Database migrations (Add-Migration, Update-Database) help evolve the schema over time.
  • This is the preferred approach in EF Core and modern .NET because it avoids the designer .edmx and keeps everything in code.

=============================================================================




=============================================================================

Thursday, September 18, 2025

CleanArchitecture

 SchoolSystemSolution 

├──  <<projectname>>.Domain                 // Enterprise business rules.  Class Library project

│   ├── Entities

│   │   ├── Student.cs

│   │   ├── Teacher.cs

│   │   └── Admin.cs

│   └── ValueObjects (if needed)

├──  <<projectname>>.Application            // Application business rules. Class Library project

│   ├── DTOs

│   │   ├── StudentDto.cs

│   │   ├── TeacherDto.cs

│   │   └── AdminDto.cs

│   ├── Interfaces

│   │   ├── IStudentRepository.cs

│   │   ├── ITeacherRepository.cs

│   │   └── IAdminRepository.cs

│   ├── Services (or UseCases)

│   │   ├── StudentService.cs

│   │   ├── TeacherService.cs

│   │   └── AdminService.cs

│   └── Mapping (optional: AutoMapper profiles)

├──  <<projectname>>.Infrastructure      // External concerns (EF, Repositories, etc.)

│   ├── Data                                            //Class Library project

│   │   └──  <<projectname>>DbContext.cs

│   ├── Repositories

│   │   ├── StudentRepository.cs   // implements IStudentRepository

│   │   ├── TeacherRepository.cs

│   │   └── AdminRepository.cs

│   └── Migrations

└──  <<projectname>>.Api                    // Framework/UI layer. WebAPI project

    ├── Controllers

    │   ├── StudentController.cs

    │   ├── TeacherController.cs

    │   └── AdminController.cs

    ├── Program.cs

    ├── appsettings.json

    └── Startup.cs (if not using minimal APIs)


Q36-Q40(EF)

Q36.  What is the difference between Include and Join in entity framework? Q37. What will be your approach in case you want to call a manual...