【发布时间】:2015-12-14 03:54:37
【问题描述】:
我正在尝试为 MVC 6 应用程序制作通用表格查看器/编辑器。
我现在用
Context.GetEntityTypes();
向我返回表格列表。
现在我需要获取特定类型的数据。我目前的实现是:
// On my context
public IQueryable<dynamic> GetDbSetByType(string fullname)
{
Type targetType = Type.GetType(fullname);
var model = GetType()
.GetRuntimeProperties()
.Where(o =>
o.PropertyType.IsGenericType &&
o.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>) &&
o.PropertyType.GenericTypeArguments.Contains(targetType))
.FirstOrDefault();
if (null != model)
{
return (IQueryable<dynamic>)model.GetValue(this);
}
return null;
}
在我的控制器中使用此代码
[HttpGet("{requestedContext}/{requestedTable}/data")]
public IActionResult GetTableData(string requestedContext, string requestedTable)
{
var data = Request.Query;
var context = GetContext(requestedContext);
if (context == null)
{
return new ErrorObjectResult("Invalid context specified");
}
var entity = context.GetEntity(requestedTable);
if (entity == null)
{
return new ErrorObjectResult("Invalid table specified");
}
var set = context.GetDbSetByType(entity.ClrType.AssemblyQualifiedName);
if (set == null)
{
return new ErrorObjectResult("Invalid table specified - DbSet could not be found");
}
var start = Convert.ToInt32(data["start"].ToString());
var count = Convert.ToInt32(data["length"].ToString());
var search = data["search[value]"];
return new ObjectResult(set.Skip(start).Take(count));
}
事实上,这将返回长度为count 和位置为start 的数据。但是我无法对IQueryable<dynamic> 的特定属性执行查询。
问题是:
- 这似乎是一件微不足道的事情,所以我几乎可以肯定我错过了什么 - 这一定很容易做到。
- 如果不是 1,那么如何将我的
object set转换回DbSet<T>以便执行查询?如果我设置断点并进行检查,我可以看到我的所有数据都放在那里。
注意:这是 EF7
附加信息:
-
requestedTable是完全限定类型 EG:<mysystem>.Models.Shared.Users
编辑 (2016/5/5)
我最终只是用纯 SQL 完成了这一切 - 如果有人确实设法让这个工作,请告诉我!
【问题讨论】:
-
如果 GetType() 实际上得到了你的类型(也就是说,引用的类型实际上是在可用的程序集中定义的)并且是在 dbContext 的集合中注册的类名,你不能只使用上下文。设置?这是一个 DbSet 并继承了 IQueryable
。
标签: c# entity-framework asp.net-core asp.net-core-mvc entity-framework-core