أسئلة القسم

تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.

Explain Entity Framework Core and its advantages. سهل

Entity Framework Core (EF Core) is Microsoft’s modern Object-Relational Mapper (ORM) for .NET. It lets you work with databases using C# classes instead of SQL directly.

Advantages:

  • Cross-platform (works with .NET Core)
  • Supports LINQ queries
  • Change tracking and relationship management
  • Migrations for schema evolution
  • Works with many databases (SQL Server, PostgreSQL, MySQL, SQLite)
What are Migrations in EF Core? سهل

A way to evolve the database schema as your models change. They keep C# models and DB schema in sync.

Example commands:

  • Add-Migration InitialCreate
  • Update-Database
What is different between Code First and Db First? سهل
Approach Code First Database First
Start point Define C# classes (models) Start from existing database schema
Schema creation EF creates/updates DB schema via migrations EF generates models from DB
Best for Greenfield projects (new DB) Legacy/existing databases
What is Database Seeding? سهل

Seeding = pre-populating the database with initial/static data. Done in OnModelCreating using .HasData().

Example:

modelBuilder.Entity<Role>().HasData(new Role { Id = 1, Name = "Admin" });
What is the difference between SaveChanges() and SaveChangesAsync()? سهل
  • SaveChanges(): Synchronous, blocks the thread until DB operation finishes.
  • SaveChangesAsync(): Asynchronous, does not block, returns a Task. Use SaveChangesAsync() in web apps for better scalability.
What is Tracking in EF Core? متوسط

Change Tracking means EF keeps track of entity states (Added, Modified, Deleted, Unchanged). Helps generate the correct SQL for SaveChanges().

What is the difference between IEnumerable and IQueryable in EF Core? متوسط
  • IEnumerable: Executes query in memory after fetching data.
  • IQueryable: Translates query to SQL and executes in DB.

Example:

// IEnumerable -> loads all then filters in memory
db.Users.AsEnumerable().Where(u => u.Age > 18);

// IQueryable -> SQL WHERE clause executed on server
db.Users.Where(u => u.Age > 18);
Explain eager loading vs lazy loading in EF. متوسط
  • Eager Loading: Related entities loaded immediately (Include()).
  • Lazy Loading: Related entities loaded when accessed (requires proxies).
How would you handle migrations in EF Core? متوسط
  • Add-Migration – create migration
  • Update-Database – apply migration
  • Remove-Migration – undo last migration Use version control to keep migrations in sync across environments.
What are Shadow Properties in EF Core? متوسط

Properties not defined in your C# entity, but tracked by EF. Useful for metadata (e.g., CreatedDate, LastModifiedBy).

What are Global Query Filters in EF Core? متوسط

Filters applied automatically to all queries for an entity. Example: Soft delete or multi-tenancy.

Example:

modelBuilder.Entity<User>().HasQueryFilter(u => !u.IsDeleted);
What is the difference between Value Objects and Entities in EF Core? متوسط
  • Entity: Has identity (Id), tracked by EF.
  • Value Object: No identity, compared by value.
What is the difference between AsNoTracking() and normal queries in EF Core? متوسط
  • Normal queries: Entities tracked by EF.
  • AsNoTracking(): Returns entities not tracked, better for read-only queries.
What are Owned Entity Types in EF Core? متوسط

Complex types owned by another entity, no separate table by default.

Example:

modelBuilder.Entity<User>().OwnsOne(u => u.Address);
Explain how Change Tracking works in EF Core. متوسط
  • EF assigns states: Added, Modified, Deleted, Unchanged.
  • Uses snapshots or proxies to detect changes.
  • On SaveChanges(), generates SQL based on state.
Explain how EF Core translates LINQ queries to SQL. متوسط
  • LINQ -> Expression Tree -> SQL via EF Core provider.

Example:

db.Users.Where(u => u.Age > 18);

becomes

SELECT * FROM Users WHERE Age > 18;
How can you log SQL queries generated by EF Core? متوسط

Using ILogger or LogTo:

optionsBuilder.LogTo(Console.WriteLine);
What are Keyless Entities (Query Types) in EF Core? متوسط
  • Entities without primary keys, used for read-only queries (e.g., views, raw SQL).
  • Defined with .HasNoKey().
How do you optimize queries in EF Core? صعب
  • Use AsNoTracking() for read-only.
  • Project only needed columns (Select).
  • Use eager loading wisely.
  • Avoid N+1 queries.
  • Use compiled queries for repeated queries.
  • Profile queries with logging/SQL Profiler.
Explain Transactions in SQL Server and how EF Core manages them. صعب
  • A transaction ensures ACID properties.
  • EF Core wraps SaveChanges() in a transaction.
  • For multiple operations:
using var transaction = db.Database.BeginTransaction();
try {
    db.SaveChanges();
    transaction.Commit();
}
catch {
    transaction.Rollback();
}
What are Interceptors in EF Core, and when would you use them? صعب
  • Interceptors = hooks into EF Core lifecycle (command execution, saving, connections).
  • Useful for logging, auditing, multi-tenancy, soft deletes.
How does EF Core support concurrency handling (Optimistic vs Pessimistic concurrency)? صعب
  • Optimistic: Assumes no conflicts, uses row version or timestamp. If conflict -> exception.
  • Pessimistic: Uses locks to prevent conflicts (via transactions).
What are Compiled Queries in EF Core, and why are they useful? صعب
  • EF normally compiles LINQ -> SQL each time.
  • Compiled queries cache the translation for reuse.
  • Improves performance for frequently executed queries.
What is the difference between Table Splitting and Inheritance (TPH, TPT, TPC) in EF Core? صعب
  • Table Splitting: Multiple entities share one table.
  • Inheritance:
    • TPH (Table-per-Hierarchy): All types in one table.
    • TPT (Table-per-Type): Separate table for each class.
    • TPC (Table-per-Concrete Type): Separate table for each concrete type, no joins.
What is the N+1 query problem in EF Core, and how do you solve it? صعب
  • Problem: One query for main entity + one query per related entity.
  • Example: Looping over users and fetching roles separately.
  • Solution: Use Eager Loading (Include) or projection with Select.