Entity Framework Core Interview Question #18

What are the main EntityState values in EF Core?

EF Core Fundamentals & DbContext Junior Beginner

Quick Interview Answer

EntityState describes how a tracked entity relates to the database.

Detailed Explanation

EntityState describes how a tracked entity relates to the database.

Added means the entity should be inserted. Unchanged means no persistence operation is currently required. Modified means one or more values should be updated. Deleted means the entity should be deleted. Detached means the entity is not being tracked by the context.

Understanding entity states is important when working with disconnected web requests and explicit state manipulation.

Code Example

var entry = db.Entry(product);

Console.WriteLine(entry.State);

entry.State = EntityState.Modified;
await db.SaveChangesAsync();