0

(this is a continuation of https://stackoverflow.com/posts/40698126)

I'm using ASP.NET Core with Entity Framework.

I'm trying to select a set of records that will be deleted, I'm not interested in accessing the data for the purpose of manipulating it further in the code.

So I amended all 4 code versions with purpose to delete one or more records, synchronously or asynchronously. Why does only the last one need a ToListAsync(), won't that actually retrieve the records from the database?

var a = db.Employee.FirstOrDefault();
db.Employee.Remove(a);
// db.Employee.RemoveRange(a); <- this also works?
db.SaveChanges();

var b = db.Employee.Where(x => x.FirstName == "Jack");
db.Employee.RemoveRange(b);
db.SaveChanges();

var c = await db.Employee.FirstOrDefaultAsync();
db.Employee.Remove(c);
await db.SaveChangesAsync();

var d = await db.Employee.Where(x => x.FirstName == "Jack").ToListAsync();
db.Employee.RemoveRange(d);
await db.SaveChangesAsync();

Do async EF queries ensure asynchronism (is this the right word?) among queries in one particular HTTP request or among all the HTTP queries an application handles at any given point in time?

Community
  • 1
  • 1
Marko
  • 1,502
  • 5
  • 21
  • 40
  • If I didn't get the question wrong... ToListAsync is creating a List from an IQueryable by enumerating it asynchronously ([MSDN](https://msdn.microsoft.com/en-us/library/dn220262(v=vs.113).aspx)). And here is a good answer explained [await ToListAsync](http://stackoverflow.com/questions/32759967/is-it-correct-if-i-am-using-await-tolistasync-over-iquerable-which-is-not-de) by @Yuval Itzchakov. – KarateJB Nov 20 '16 at 16:31
  • @Marko: I'm not sure what you mean. All of them can use `SaveChangesAsync` if you want to. – Stephen Cleary Nov 21 '16 at 01:24

0 Answers0