
What are Entity States in EF Core?
Entity States gets the current status of an Entity using DBContext.
What is Change Tracker in EF Core?
The Change Tracker is responsible for tracking entity instances and their states.The change tracker monitors the entity instances and make sure that
database is updated accordingly.This Tracking mechanism is neccesary for EF core to know which entities must be inserted,updated or deleted in database.
For example, when you query the database, EF Core automatically starts tracking returned entities.
using (var dbcontext=new APPDBContext())
{
var busdetails= await dbcontext.busdetails.ToListAsync();
}
How to disable Change Tracker?
when you just need to read from database and you dont need any updation, you can very well inform EF Core to "Not Track these Entities" in Change Tracker.
Especially it is usefull, when you to retrieve lots of records from the database and you dont need to update them.
The AsNoTracking method is used to query entities without tracking them.This can improve application performance
using (var dbcontext=new APPDBContext())
{
var busdetails= await dbcontext.busdetails.AsNoTracking().ToListAsync();
}
How to Track All Entities in EF Core?
You can retrieve all tracked entities using Entries method.
var trackedentities=dbcontext.ChangeTracker.Entries();
foreach(var entry in trackedEntites)
{
console.writeline($"Entity{entry.Entity},State:{entry.State}")
}
The Five Entity States in EF Core:-
1.Added:- If you want to add new Entity and hasn't been saved in database yet.EF Core will generate INSERT statement for this entity.
var busdetails = new Busdetails { BusName = "YBMTRAVELS" };
context.busdetails.Add(busdetails); // OR
context.Entry(busdetails).State = EntityState.Added;
2.Modified:-
It means the Entry exists in the database and some properties have changed.
EF Core automatically generate UPDATE statement.
var busdetails=context.busdetails.Find(1);
busdetails.busname="SBMTRAVELS";
context.Entry(busdetails).State=EntityState.Modified;
EntityState.Modified will update all properties, not only changed ones.
3.Deleted:-
The Entity should be removed from the database.
EF Core generates a DELETE command.
context.busdetails.Remove(busdetails);
context.Entry(user).State = EntityState.Deleted;
4.UnChanged:-
The entity is tracked by context, but no changes have been made.
var busdetails = context.busdetails.AsNoTracking().FirstOrDefault();
// It will not be updated or deleted unless explicitly marked.
5.Detached:- The Entity is not being tracked by the context or the entity should not be tracked and will be removed from the change tracker
var busdetails = new busdetails { Id = 1 };
context.Entry(busdetails).State == EntityState.Detached;
How to Check current Entity State?
You can inspect and modify entity state using DbContext.Entry():
var entry = context.Entry(busdetails);
Console.WriteLine(entry.State); // Check current state
entry.State = EntityState.Modified; // Manually mark as modified
For example, we will create following entities for understanding EntityStates.
Real world example of using Change Tracker.
public class Busdetails
{
public int busid{get;set;}
public string busfrom{get;set;}
public string busto{get;set;}
public date journeydate{get;set;}
public string busname{get;set;}
public string boardingpoint{get;set;}
public string destinationpoint{get;set;}
public virtual Icollection<BusFeatures> BusFeatures{get;set;}
}
public class BusFeatures
{
public int busfid{get;set;}
public string busfeature{get;set;}
public int busid{get;set;}
public virtual Busdetails{get;set;}
}
using (var dbContext = new ApplicationDbContext())
{
var busdetails = new Busdetails
{
Id = Guid.NewGuid(),
busname = "zingbus",
boardingpoint = "Chennai",
destinationpoint="Delhi"
};
dbContext.busdetails.Add(busdetails);
await dbContext.SaveChangesAsync();
busdetails.busname = "Nationaltravels";
busdetails.boardingpoint = "Chennai",
busdetails.destinationpoint="Mumbai"
await dbContext.SaveChangesAsync();
}