开发者

GetTable equivalent for ObjectContext

开发者 https://www.devze.com 2023-04-01 08:17 出处:网络
I was previously using a DataContext which had a GetTable(type) method to get tables generically.Example:

I was previously using a DataContext which had a GetTable(type) method to get tables generically. Example:

context.GetTable(myObject.GetType());

Recently my team decided to switch to using ObjectContext with the Entity Framework. Is there a way to get tables by the entity name similar to DataContexts GetTable method without h开发者_如何学编程aving to specify a specific type? It has to be generic.


There is indeed a very simple way to do this, like so:

public IQueryable GetTable<T>(T entity) where T : class
{
    return context.CreateObjectSet<T>();
}

Now if I create a Person object and pass to it the generic method, the variable below 'allPeople' will be an IQueryable of people from my database which you can iterate though.

Person person = new Person();
IQueryable allPeople = GetTable(person);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号