Posts

Showing posts with the label EF Core

📌 Indexes in EF Core

Indexes are one of the most powerful tools for query performance tuning. In Entity Framework Core (EF Core), indexes help: ⚡ Speed up lookups, filtering, and sorting 🔒 Enforce uniqueness 🏢 Support multi-tenant and domain-specific rules This guide walks through everything you need to know — from the basics to advanced scenarios — with real-world code samples and best practices. ✅ What is an Index in EF Core? An index is a database object that allows the database to locate rows faster. Use .HasIndex() → to create indexes via Fluent API Use .IsUnique() → to enforce uniqueness EF Core automatically creates indexes for foreign key columns ✅ Defining Indexes in EF Core Type Description Basic Index Creates a non-unique index on the Name column. Unique Index Ensures no two rows share the same Sku (enforced at the DB level). Composite Index (Multi-Column) Used when queries filter on multiple fields together. Example: WHER...

🔑 Keys in EF Core

✅ 1. Primary Key (PK) Definition: Uniquely identifies each entity. Default Convention: Property named Id or <EntityName>Id . Explicit Definition: Data Annotation: [Key] public int ProductId { get; set; } Fluent API: modelBuilder.Entity<Product>() .HasKey(p => p.ProductId); ✅ 2. Composite Key modelBuilder.Entity<OrderDetail>() .HasKey(od => new { od.OrderId, od.ProductId }); ⚠️ Must be defined using Fluent API (Data Annotations don’t support composite keys). ✅ 3. Alternate Key modelBuilder.Entity<Product>() .HasAlternateKey(p => p.Sku); ✅ Primary Key vs Alternate Key Feature Primary Key Alternate Key Uniquely identifies? ✅ Yes ✅ Yes Required? ✅ Yes ❌ Optional Can be used as FK? ✅ Yes ✅ Yes (with .HasPrincipalKey ) ✅ 4. Foreign Key (FK) modelBuilder.Entity<Employee...

EF Core Coding Example

EF Core Coding Example Shadow properties in EF Core: Define shadow properties for tracking audit information  and Automatically set the current date when created protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Product>() .Property<DateTime>("CreatedDate") .HasDefaultValueSql("GETDATE()"); } AsNoTracking method in EF Core: Method to get data without tracking changes and Using AsNoTracking to improve performance and prevent change tracking for a read-only operation. public List<Product> GetAllProducts() { return _context.Products.AsNoTracking().ToList(); } public Product GetProductById(int id) { return _context.Products.AsNoTracking().FirstOrDefault(p => p.Id == id); } What is the importance of DbContext disposal, and how do you manage it effectively? // Register DbContext with scoped lifetime builder.Services.AddDbContext<StoreDbContext>(options =>     options.UseSqlServer(builder.Conf...

EF Core technical coding interview questions

EF Core technical coding interview questions 1. How do you configure a one-to-many relationship in EF Core? Question : How would you configure a one-to-many relationship between Author and Book in EF Core, where one author can have many books, but a book can only have one author? Answer : In EF Core, a one-to-many relationship can be configured using the HasMany and WithOne methods in the OnModelCreating method. public class Author { public int AuthorId { get; set; } public string Name { get; set; } public ICollection<Book> Books { get; set; } } public class Book { public int BookId { get; set; } public string Title { get; set; } public int AuthorId { get; set; } public Author Author { get; set; } } public class AppDbContext : DbContext { public DbSet<Author> Authors { get; set; } public DbSet<Book> Books { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Ent...