How to return a generic entity using EF6 datacontext?
-
21-12-2019 - |
Question
I have a simple database with a 'Person
' and a 'Subscription
' table where each person has one or more subscriptions.
private MyDBContext_dbContext;
public T GetByID(int ID)
{
return _dbContext.Mapping. ??
}
Now the intention was to create a generic method to return either a Person entity or a Subscription entity from the datacontext based on the type provided.
In Linq2SQL I was able to use .GetTable<T>
method, how can I do the same here with EntityFramework 6? The .Mapping
part isn't even recognized by intellisense, so i am missing something basic here.
Thanks
Solution
DbContext offers you Set method. You may either use it as generic method:
DbSet<Person> people = DbContext.Set<Person>();
or non-generic:
DbSet<Person> people = DbContext.Set(typeof(Person)) as DbSet<Person>;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow