entity-framework - 从字符串获取 DbSet<MyTable>

标签 entity-framework

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/

相关文章:

sql-server-2008 - Entity Framework 代码优先权限/数据库创建问题

entity-framework - EF5 视觉设计更新但模型不是

c# - 从 Entity Framework 5 到 MySQL 的存储过程调用

ios - 核心数据 - 如何跟踪实体集的子集

entity-framework - EF Code First、IoC 和 DbConnection

entity-framework - Entity Framework 代码优先和显式接口(interface)

c# - 如何在 C# 中使用 lambda 表达式进行嵌套 where 查询

c# - "The operation cannot be completed because the DbContext has been disposed"

entity-framework - Entity Framework 4 : How expensive is it to create an EntityConnection?

entity-framework - EF Code First 给出错误:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'People' 中插入标识列的显式值。