C#,ado.net,Entity Framework里面的泛型问题。

2024-11-29 00:56:16
推荐回答(2个)
回答1:

en.CreateObjectSet().ToList(); 

另外其实 你的实参是多余的,泛型查询类你可以这么写:

  public class EntityProvider : IDisposable where T : EntityObject
    {
     public void Dispose()
        {
            if (this.entForQueryOnly != null)
            {
                this.entForQueryOnly.Dispose();
            }
        }
 // 仅查询用
        protected NorthwindEntities entForQueryOnly;
 public virtual List GetList()
        {
            return this.entForQueryOnly.CreateObjectSet().ToList();
        }

        public virtual List GetList(System.Linq.Expressions.Expression> predicate)
        {
            return this.entForQueryOnly.CreateObjectSet().Where(predicate).ToList();
        }
        //Add
        public virtual T Add(T entity)
        {
            using (NorthwindEntities ent = new NorthwindEntities())
            {
                try
                {
                    ent.CreateObjectSet().AddObject(entity);
                    ent.SaveChanges();
                    return entity;
                }
                catch
                {
                    return default(T);
                }
            }
        }
 }

回答2:

如果你不用反射Relection,恐怕是找不到答案的.

思路应该是

  1. Type type= t.GetType();

  2. 确定Type以后再利用反射从en中找数据集

   没试过,自己试试吧.