GetTable equivalent for ObjectContext

点点圈 提交于 2019-11-28 11:58:57

问题


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 having to specify a specific type? It has to be generic.


回答1:


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);


来源:https://stackoverflow.com/questions/7263083/gettable-equivalent-for-objectcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!