gpt4 book ai didi

entity-framework - 从字符串中获取 DbSet

转载 作者:行者123 更新时间:2023-12-04 05:24:18 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com