c# – 从类型获取DbSet

c# – 从类型获取DbSet,第1张

概述我正在尝试为MVC 6应用程序制作一个通用的表查看器/编辑器. 我目前使用 Context.GetEntityTypes(); 给我一张表的清单. 现在我需要获取特定类型的数据.我目前的实施是: // On my contextpublic IQueryable<dynamic> GetDbSetByType(string fullname){ Type targetType = Ty 我正在尝试为MVC 6应用程序制作一个通用的表查看器/编辑器.

我目前使用

Context.GetEntityTypes();

给我一张表的清单.

现在我需要获取特定类型的数据.我目前的实施是:

// On my contextpublic 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));}

就这样,这将返回长度数据和位置开始的数据.但是,我无法对Iqueryable< dynamic>的特定属性执行查询.

问题是:

>这似乎是一件小事,所以我几乎肯定我错过了一些东西 – 这一定很容易做到.
>如果不是1,那么我如何将我的对象集转换回DbSet< T>所以我可以执行我的查询?如果我设置了一个断点和检查,我可以看到我所有的数据只是坐在那里.

注意:这是EF7

附加信息:

> requestedtable是完全限定类型EG:< mysystem> .Models.Shared.Users

编辑(2016/5/5)

我最终只是用简单的sql来做这些 – 如果有人设法得到这个工作,请让我知道!

解决方法 这将通过使用通用方法和使用DbContext.Set< TEntity>()来简化.您可以在运行时创建一个通用的方法,如下所示:
public IActionResult GettableData(string requestedContext,string requestedtable){    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 boundMethod = s_gettableDataMethodInfo.MakeGenericmethod(entity.ClrType);    return boundMethod.Invoke(this,new object[] { context }) as IActionResult;}private static Readonly MethodInfo s_gettableDataMethodInfo    = typeof(MyController).GetTypeInfo().GetDeclaredMethod("GettableDataForEntity");private IActionResult GettableDataForEntity<TEntity>(DbContext context)    where TEntity : class{    var data = Request.query;    var start = Convert.ToInt32(data["start"].ToString());    var count = Convert.ToInt32(data["length"].ToString());    var search = data["search[value]"];    return new ObjectResult(context.Set<TEntity>().Skip(start).Take(count));}
总结

以上是内存溢出为你收集整理的c# – 从类型获取DbSet全部内容,希望文章能够帮你解决c# – 从类型获取DbSet所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1244095.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存