Insert data using Entity Framework model
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Entity Framework, inserting data means creating an entity object, adding it to the relevant DbSet, and calling SaveChanges or SaveChangesAsync. The framework tracks the new entity, generates the required SQL INSERT, and updates generated keys back onto the object after the database commit.
Basic Insert Flow
A minimal EF Core model looks like this:
To insert a row:
After SaveChanges, EF sends the insert to the database and populates customer.Id if the key is database-generated.
What Add Actually Does
Add does not immediately talk to the database. It marks the entity state as Added inside the change tracker. The actual SQL is generated later when you call SaveChanges.
That distinction matters because you can stage several inserts first:
EF then writes both changes in one save operation.
Async Insert
In web applications and other scalable server code, use the async path:
Async does not change the data model. It only changes how the database work is awaited.
Inserting Related Data
EF also handles relationships. If an order belongs to a customer, you can insert both through navigation properties:
EF tracks both entities and inserts them in the required order.
Validate Before Saving
Entity Framework is not a substitute for input validation. Check required fields, uniqueness rules, and domain constraints before calling SaveChanges. Database exceptions are still possible, but good validation keeps them from becoming your normal control flow.
Keep DbContext Lifetime Short
A DbContext is designed for a unit of work, not for the lifetime of the whole application process. Use one context for one operation, request, or transaction scope, then dispose it.
That keeps tracking state small and prevents stale entities from accumulating.
Common Pitfalls
The most common mistake is creating an entity object and forgetting to call SaveChanges, which means nothing is written to the database. Another is keeping one DbContext alive too long and then wondering why stale tracked entities or unexpected updates appear.
Developers also sometimes expect Add itself to execute SQL immediately. It does not. Finally, if the database generates keys, do not assume the entity has its final key value until the save operation completes.
Summary
- Create the entity, add it to the
DbSet, then callSaveChangesorSaveChangesAsync. - '
Addonly marks the entity as new; the database write happens during save.' - EF can insert multiple related entities in one tracked unit of work.
- Keep
DbContextlifetimes short and validate data before saving. - Database-generated keys are usually available on the entity after the save succeeds.
Related reading
- INSERT IGNORE vs INSERT ... ON DUPLICATE KEY UPDATE
- INSERT IGNORE vs INSERT ... ON DUPLICATE KEY UPDATE
- INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE
- Insert into a MySQL table or update if exists
- Inserting a tab character into text using C
- Install a .NET windows service without InstallUtil.exe
- Insert into a MySQL table or update if exists
- Insert, on duplicate update in PostgreSQL?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.