基于表名称(实体框架)获取表 (对象集)
本文关键字:获取 对象 框架 于表名 实体 | 更新日期: 2023-09-27 18:37:05
我的目标是使用实体框架(也许是反射)来创建这样的函数:
public List<MyRows> getTableByName(string tableName)
{
ObjectQuery objectQuery = (ObjectQuery)getObjectSet(tableName, m_context); // How do you do this part???
List<MyRows> rows = new List<MyRows>();
SortByColumn(objectQuery); // I can do this myself.
MyWhereClauses(objectQuery); // I can do this myself.
rows = ConvertToMyRows(objectQuery.ToList()); // I can do this myself.
}
类"MyRows"是我发明的,所以我可以添加更多属性。"ConvertToMyRows"、"MyWhereClauses"和"getObjectset"也是虚构的函数。
这种事情在实体框架中可能吗?
如何基于模板 T 或表名字符串执行某种"getObjectSet"类型的函数?
我厌倦了编写具有特定功能的数据访问文件只是为了生成项目列表。
解决:
public IQueryable<T> GetTable<T>() where T : class
{
var table = m_entities.CreateObjectSet<T>();
return table;
}
public string MyData<T>() where T: class { var table = GetTable<T>(); //order here }
public IQueryable<T> GetTable<T>() where T : class
{
var table = m_entities.CreateObjectSet<T>();
return table;
}
public string MyData<T>() where T: class {
var table = GetTable<T>(); //order here
}