获取DbSet< MyTable>从一个字符串 [英] Getting DbSet<MyTable> from a string

查看:48
本文介绍了获取DbSet< MyTable>从一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ADO.NET用户首次尝试EF。我正在尝试从表名称的字符串中获取表。
比我想象的要难。
基本上我在这里:

ADO.NET user trying EF for the first time. I'm trying to get a table from a string of the table name.. Harder than I presumed. Basiclly I'm here:

var tableName = "Name.Entities.Measure" + measureType;
var table = Activator.CreateInstance("Name.Entities", tableName);
var unwrapped = table.Unwrap();
var type = unwrapped.GetType();
var dbset = context.Set<type>();

OR

switch (tableString)
{
    case "table1":
    return GetDataFromTable1();
    case "table2":
    return GetDataFromTable2();
}

代码少就好了;)

考虑使用所有实体的可搜索集合制作扩展方法。
任何人都可以找到更好的解决方案?

Thinking of making an extension method with a searchable collection of all the entities. Better solution anyone?

推荐答案

我一直在寻找相同的东西。尝试使用Set()的非泛型版本:

I was looking for the same thing. Try the non-generic version of Set():

var tableName = "Name.Entities.Measure" + measureType;
var type = Type.GetType(tableName);
var dbset = Context.Set(type);

这篇关于获取DbSet&lt; MyTable&gt;从一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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