【问题标题】:Entity Framework, Dynamic requested entity实体框架,动态请求的实体
【发布时间】:2013-04-05 05:52:21
【问题描述】:

我有一个使用 EF 5.0.0 映射的数据库

我正在一些列表中显示数据。我的数据是以“经典方式”检索的:

using (myContext db = new myContext())
{

    var products = db.products.Select(p => p).ToList();
}

我必须尽可能多地这样做,

有什么办法可以动态地做到这一点? :

using (myContext db = new myContext())
{
    type currentType = myTable1Type
    var currentList = db.currentType.Select(p => p).ToList();
}

【问题讨论】:

    标签: .net database linq entity-framework mapping


    【解决方案1】:

    尝试使用DbContext.Set Method (Type):

    using (myContext db = new myContext())
    {
        type currentType = myTable1Type
        var currentList = db.Set(currentType).Select(p => p).ToList();
    }
    

    【讨论】:

      【解决方案2】:

      看看 Context.Set() 方法组。

      【讨论】:

        【解决方案3】:

        这个方法对我有用:

        IDbSet GetDbSet(Type currentType, Context db)
        {
            dynamic instance = Activator.CreateInstance(currentType);
            return GetDbSetFromInstance(instance, db);
        }
        
        IDbSet GetDbSetFromInstance<T>(T instance, Context db)
            where T : class
        {
            var set = db.Set<T>();
            if (set == null)
            {
                throw new Exception();
            }
            return set;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-10
          • 1970-01-01
          • 2013-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多