Just wanted peoples opinions on a scenario:
Would it be more efficient to:
Or is this trivial?
My scenario is that we have a health check routine that does a select on a table every 10 seconds and very rarely needs to be updated (Only updates the record if a new version has been deployed). So should we do the health check with change tracking turned off turned on?
According to your use case, I think No-tracking queries will give big performance boost to your app.
So you can do that using AsNoTracking()
using (var context = new HelthContext())
{
    var patients = context.Patients.AsNoTracking().ToList();
}
If you have an entity that you know already exists in the database but which is not currently being tracked by the context then you can tell the context to track the entity using the Attach method on DbSet as shown below.
var existingPatient = new Patient { Id = 1, Name = "Patient 1" }; 
using (var context = new HelthContext()) 
{ 
    context.Patients.Attach(existingPatient ); 
    // Do some more work...  
    context.SaveChanges(); 
}
Reference : Entity states and SaveChanges
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With