wk4questions
wk4questions

Reputation: 135

Entityframework generic way to access entity

is there a way to make this generic

Context.SalesEntity.Where(t=>t.id==3).Delete();

something like

private void DoWork<T>(Expression<Func<T, bool>> predicate)
{
  Context.T.Where(predicate).Delete();
}

I have already tried the predicate bit seems to work ok. but I have no idea how to do the context.entity bit generically.

Upvotes: 2

Views: 479

Answers (2)

Julius Depulla
Julius Depulla

Reputation: 1533

private void DoWork<T>(Expression<Func<T, bool>> predicate)
{
  Context.Set<T>().Where(predicate).Delete();
}

Upvotes: 2

Wouter de Kort
Wouter de Kort

Reputation: 39898

You can use the Context.Set<T> method. This returns a DbSet<T> of the specified type.

Upvotes: 4

Related Questions