我有以下代码(示例):
public dynamic GetData(string name)
{
using(var ctx = GetObjectContext())
{
switch (name)
{
case "entity1":
return ctx.entity1.ToList();
case "entity2":
return ctx.entity2.ToList();
......
default:
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想避免在此示例中切换.如何按名称查找所需的实体类,调用ToList()方法并返回数据?我可以用反射做到这一点吗?你能帮助我吗?
我有DbContext几个DbSet<T>属性:
public virtual DbSet<A> A { get; set; }
public virtual DbSet<B> B { get; set; }
public virtual DbSet<C> C { get; set; }
...
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我现在必须能够以实体名称作为字符串检索特定的DbSet(例如,当用户输入"A"时,我需要获取Dbset<A>).
在以前的EF版本中,以下是可能的:
var dbset = Context.Set(Type.GetType(A));
使用当前版本的EF核心有类似的方法吗?我已经尝试了几种方法来实现这一点,但我现在使用它的唯一方法是使用一个相当丑陋的开关/案例,我想摆脱它.
我在这里发现了几个类似问题的帖子,但它们都与早期的.NET Core版本或EF5/EF6有关.
我有一个接收像string这样的实体的方法来返回该实体的列表.
我想要的是动态设置实体并返回结果.
我需要的是这样的:
DbContext.Set <Type.GetType("Entity")>().Select(e => e);
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
也许这样做不正确.
该Set方法来自Entity Framework Core.
c# asp.net-web-api entity-framework-core .net-core asp.net-core-webapi
我正在尝试使用它的名称动态System.Reflections获取DbSet<T>。
我现在有的是:
DbSet名DbSet的Type存储在可变在尝试使用该dbcontext.Set<T>()方法时,我面临的问题出现了,因为(这些是我迄今为止的尝试):
<T>my 时DbSet Type,它会引发以下编译错误:
“XXX 是一个变量,但像类型一样使用”
Extension方法,您将在我的代码下面找到(我为了设法弄一本制作IQueryable<T>),它返回一个IQueryable<object>,不幸的是不是我期待的,因为当然,当我尝试用进一步的反射操作它,它缺少原始类具有的所有属性......我究竟做错了什么?我怎样才能得到一个DbSet<T>?
我的代码如下,当然,如果您需要更多信息、说明或代码片段,请告诉我。
我的控制器的方法:
public bool MyMethod (string t, int id, string jsonupdate)
{
string _tableName = t;
Type _type = TypeFinder.FindType(_tableName); //returns the correct type
//FIRST TRY
//throws error: "_type is a variable but is used like a type"
var tableSet = _context.Set<_type>();
//SECOND …Run Code Online (Sandbox Code Playgroud) 需要通过将表名作为参数(在DB第一种方法中生成的模型并使用EF 6.0)动态创建实体框架生成的Model类的实例
喜欢,
// Input Param
string tableName
// Context always same
DBContext dbContext= new DBContext();
//Need to create object query dynamically by passing
//table name from front end as below
IQueryable<"tableName"> query = dbContext."tableName ";
Run Code Online (Sandbox Code Playgroud)
需要传递100多个表作为输入参数并且所有表的结构相同.
请帮忙.
我正在尝试LINQ使用动态表名执行一些命令。例如,代替:
var o = (from x in context.users select x);
Run Code Online (Sandbox Code Playgroud)
我想使用类似:
var o = (from x in getTableObjectByName("users", context) select x);
Run Code Online (Sandbox Code Playgroud)
或多或少。到目前为止,这是我编译和运行的代码:
using (MySiteEntities ipe2 = new MySiteEntities()) {
var propinfo1 = Type.GetType("MySiteNamespace.MySiteEntities").GetProperty("users");
var propval1 = propinfo1.GetValue(ipe2, null);
}
Run Code Online (Sandbox Code Playgroud)
运行,但始终返回零记录。用户表最明确地包含记录,无论如何,当我直接使用上述第一种方法直接调用它时,都会得到所有记录。如何修改代码以实际提取记录,而不仅仅是空集合?
编辑:我也尝试过:
using (MySiteEntities ipe = new MySiteEntities())
{
var prop = Type.GetType("MySiteNamespace.MySiteEntities").GetProperty("users");
Type dbsetType = typeof(DbSet<>);
dbsetType = dbsetType.MakeGenericType(Type.GetType("MySiteNamespace.user"));
Type t = dbsetType.GetType();
var val = prop.GetValue(ipe, null);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,代码不仅会运行,而且实际上会按预期返回结果。但是,val是一个对象。我需要将其强制转换为type DbSet<user>,这很容易,除了该参数user仅在运行时才知道....强制转换也必须是动态的。我尝试使用Convert.ChangeType(val, …