Use of Include with async await

Find() and FindAsync() are methods on type DbSet (which is what db.Items is). Include() returns a DbQuery object, which is why FindAsync() is not available. Use SingleOrDefaultAsync() to do the same thing as FindAsync() (the difference is it will go straight to the database and won’t look in the context to see if the entity exists first)…

Item item = await db.Items.Include("Tags").SingleOrDefaultAsync(i => i.Id == id);

Leave a Comment