Blog
Mert Özen's blog posts on software development, backend, frontend, and DevOps.
EF Core in ASP.NET Core: AddDbContext, Scoped Lifetime, Factory and Pooling
We wire DbContext into ASP.NET Core with AddDbContext, watch the scoped lifetime through ContextId, solve the singleton and background service traps, and see with real output when AddDbContextFactory and AddDbContextPool help.
Raw SQL in EF Core: FromSqlRaw, FromSqlInterpolated, Keyless Entities and SQL Injection
We look at FromSqlRaw versus FromSqlInterpolated, the SQL produced when LINQ is composed on top, the SQL injection trap with $, ExecuteSql commands, keyless entities mapped to views, and calling stored procedures on SQL Server, all with real output.
Concurrency in EF Core: Tokens, rowversion and DbUpdateConcurrencyException
We reproduce a lost update, set up optimistic concurrency with a concurrency token, use rowversion on SQL Server and a Guid version column on SQLite, and resolve DbUpdateConcurrencyException with a retry loop.
Transactions in EF Core: SaveChanges, BeginTransaction and Savepoints
We watch SaveChanges' implicit transaction in the logs, turn several SaveChanges calls into one unit with BeginTransaction, roll back part of the work with savepoints, and look at the pitfalls of TransactionScope and retry strategies.
Saving Data with EF Core: SaveChanges, Related Graphs and Disconnected Entities
The two worlds of saving data in EF Core: Add/Remove and SaveChanges writing only changed columns in the connected scenario, batching on SQLite and SQL Server, saving related graphs and orphans; Update writing every column, load-and-change, SetValues, Attach and IsModified in the disconnected scenario.
Change Tracking in EF Core: Entity States, AsNoTracking and Identity Resolution
We open up EF Core's change tracker: the five entity states, ChangeTracker.Entries and DebugView, the one-object-per-row rule (identity resolution), AsNoTracking vs AsNoTrackingWithIdentityResolution, when DetectChanges runs, and the 'already being tracked' error.
Loading Related Data in EF Core: Include, Explicit and Lazy Loading, Split Queries
We compare the three ways to load related data in EF Core with real SQL output: Include/ThenInclude and filtered include, explicit loading with the Entry API, lazy loading with proxies and its N+1 bill; then cartesian explosion and AsSplitQuery.
LINQ Queries with EF Core: SQL Translation, Client Evaluation and the IQueryable Trap
We follow how an EF Core LINQ query becomes SQL: deferred execution, ToQueryString and LogTo, filtering, sorting and paging, projection, First/Single/Find, client evaluation allowed in the final Select, and the IEnumerable trap that silently pulls the whole table.
Seeding in EF Core: Model Seeding with HasData and Application-Side Seeding
Two ways to add initial data in EF Core: model seeding with HasData and the InsertData/UpdateData/DeleteData it generates, the key and navigation rules, the dynamic value trap; then a rerunnable application-side seeder and a pointer to EF Core 9's UseSeeding.
EF Core Migrations: migrations add, database update, Scripts and Bundles
We run EF Core migrations end to end: migrations add/remove, database update and reverting, the anatomy of a migration file, SQL scripts and --idempotent, EF Core 6 migration bundles, and the risks of calling Migrate() at startup.
Relationships in EF Core, Part 2: One-to-One, Many-to-Many and UsingEntity
We build one-to-one with Author and AuthorProfile and many-to-many with Book and Tag: the unique foreign key index, the dependent-side error and HasForeignKey<T>, skip navigations and the BookTag table created by convention, naming the join table with UsingEntity and a join entity with a TaggedAt payload.
Relationships in EF Core, Part 1: One-to-Many, Foreign Keys and Cascade Delete
We build a one-to-many relationship with Author and Book: principal, dependent and foreign key, how the convention finds the relationship, HasOne/WithMany in the Fluent API, adding graphs and fixup, required versus optional relationships, and real SQLite output for Cascade, ClientSetNull and Restrict with loaded and unloaded dependents.