I have a DbContext which has a number of DbSets in it. I would like to check whether an entity of type T exists in the DbContext with a particular Id.
At the moment, I have this code
public async Task<bool> EntityExists<T>(int id) where T : class
{
return await db.FindAsync<T>(id) != null;
}
Which works as expected - problem is, I don't want to return the entity itself, for optimisation purposes, since I'm not using it - I just want to see if it exists or not.
I know that AnyAsync can be used with dbSets - so
public async Task<bool>EntityFooExists(int id)
{
return await db.Foo.AnyAsync(f => f.id == id);
}
would work, but I don't want to have to create a function for each dbset.
So, is there a way I can do this without retrieving the entire entity?