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

查看:20
本文介绍了等效于 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 和实体框架.有没有一种方法可以通过类似于 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 对象并将通用方法传递给它,'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天全站免登陆