ADO.NET 用户首次尝试 EF。我正在尝试从表名字符串中获取一个表。 比我想象的还要难。 基本上我在这里:
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>();
或者
switch (tableString)
{
case "table1":
return GetDataFromTable1();
case "table2":
return GetDataFromTable2();
}
代码越少越好;)
考虑创建一个具有所有实体的可搜索集合的扩展方法。 有人有更好的解决方案吗?
最佳答案
我一直在寻找同样的东西。尝试 Set() 的非通用版本:
var tableName = "Name.Entities.Measure" + measureType;
var type = Type.GetType(tableName);
var dbset = Context.Set(type);
关于entity-framework - 从字符串获取 DbSet<MyTable>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13376975/