ObjectContext的GetTable等效 [英] GetTable equivalent for ObjectContext

查看:112
本文介绍了ObjectContext的GetTable等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前使用过一个DataContext,它有一个GetTable(type)方法来获取表。示例:

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

context.GetTable(myObject.GetType());



最近我的团队决定切换到使用ObjectContext与Entity框架。有没有办法获得表的实体名称类似于DataContexts的GetTable方法,而不必指定一个特定的类型?它必须是通用的。

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.

推荐答案

确实有一个非常简单的方法来这样做:

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

现在如果我创建一个 Person object并传递给它的通用方法,'allPeople'下面的变量将是一个 IQueryable 的人从我可以迭代的数据库。

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

这篇关于ObjectContext的GetTable等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆