Mr. Boy
Mr. Boy

Reputation: 63710

Is there a generic way to test if an object exists in the DB?

EF6 provides generic ways to control DB state through DbContext.Entry(object).XXX as opposed to DbContext.Objects.XXX

Is there a way to do this generically to test if an object exists? All I've come across would be calling DbContext.Objects.Any(...) and since my code-base is generic across about 6 different types this is a shame.

Upvotes: 2

Views: 86

Answers (1)

ocuenca
ocuenca

Reputation: 39326

You could use Set<TEntity> generic method:

public bool Exist<T>(Expression<Func<T,bool>> condition)
{
   return context.Set<T>().Any(condition);
}

Upvotes: 2

Related Questions