❔ Updating an entity/record with 1 query(?) on Entity Framework

My senior told me that there's a way to select and update something in EF in 1 query. So instead of this:
void MyMethod(int id)
{
BundleRecord bundle = await databaseContext.Bundles.TagWithCallerName().SingleOrDefaultAsync(x => x.Id == id);
bundle.ScanUploaded = true;

await databaseContext.SaveChangesAsync();
}
void MyMethod(int id)
{
BundleRecord bundle = await databaseContext.Bundles.TagWithCallerName().SingleOrDefaultAsync(x => x.Id == id);
bundle.ScanUploaded = true;

await databaseContext.SaveChangesAsync();
}
He said, if he remembers correctly, I should use the Attach() method. I've been Googling, and this is how I understood how to use this method:
void MyMethod(int id)
{
BundleRecord newBundle = new()
{
Id = id,
ScanUploaded = true // eliminates the "bundle.ScanUploaded = true;" line?
};

databaseContext.Bundles.Attach(newBundle).State = EntityState.Modified;

await databaseContext.SaveChangesAsync();
}
void MyMethod(int id)
{
BundleRecord newBundle = new()
{
Id = id,
ScanUploaded = true // eliminates the "bundle.ScanUploaded = true;" line?
};

databaseContext.Bundles.Attach(newBundle).State = EntityState.Modified;

await databaseContext.SaveChangesAsync();
}
Just wondering if my approach and understanding of the Attach() method are correct.
11 Replies
Saber
Saber2y ago
I would not recommend using attach and modifying the state direction. I wouldn't be suprised if that wiped all the other fields. If your on ef core 7, there are built in methods to do this, otherwise linq2db offers some extension methods to do this as well without having to manually play with the change tracker
absolution183
absolution183OP2y ago
I'm curious why he would suggest this, instead of just doing the first approach. Is the first approach not optimized or something?
Saber
Saber2y ago
it just has an extra database call
absolution183
absolution183OP2y ago
Doing SELECT and UPDATE instead of just UPDATE?
absolution183
absolution183OP2y ago
So I ended up seeing Entry() method as well, and this SO answer says that's the one that updates all fields, and Attach() is actually the safer method. https://stackoverflow.com/a/30988176
Stack Overflow
DbSet.Attach(entity) vs DbContext.Entry(entity).State = EntityState...
When I am in a detached scenario and get a dto from the client which I map into an entity to save it I do this: context.Entry(entity).State = EntityState.Modified; context.SaveChanges(); What is the
Angius
Angius2y ago
ExecuteUpdate() in EF Core 7
absolution183
absolution183OP2y ago
Looks like I'm on EF Core 5
Angius
Angius2y ago
You have two choices then. One involves fetching and saving, one involves attaching an entity First choice is more proper but takes two db calls Second opens you up for a lot of issues, but saves you a db call
Saber
Saber2y ago
or use linq2db extensions to do it
Angius
Angius2y ago
Or that, yea
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server