예제 #1
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
 /// <summary>
 /// 使用命名参数查询,尽量不要使用此方法, 可能会一次性查询出大量的数据, 应该使用分页查询通用方法
 /// SqlQuery(select * from article keywords like @p0, "%"+keywords+"%")
 /// </summary>
 /// <typeparam name="T">需要得到的类型, 查询哪张表(必须和表名称一致)</typeparam>
 /// <param name="sql">查询语句,参照注释</param>
 /// <param name="param">查询参数</param>
 /// <returns></returns>
 public static List <T> selectList <T>(string sql, params object[] param) where T : class, new()
 {
     using (WeiQingEntities db = new WeiQingEntities())
     {
         var set  = db.Set <T>();
         var list = set.SqlQuery(sql, param).ToList();
         return(list);
     }
 }
예제 #2
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
 /// <summary>
 /// 使用泛型保存对象到数据库中, 类型参数可以是 基类, 参数model对象可以是 扩展类对象
 /// </summary>
 /// <typeparam name="T">基类</typeparam>
 /// <param name="model">子类对象</param>
 /// <returns></returns>
 public static int insert <T>(T model) where T : class
 {
     using (WeiQingEntities db = new WeiQingEntities())
     {
         DbSet <T> set = db.Set <T>();
         set.Add(model);
         return(db.SaveChanges());
     }
 }
예제 #3
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
 /// <summary>
 /// 使用泛型删除数据库中的对象, 类型参数可以是 基类, 参数model对象可以是 扩展类对象
 /// </summary>
 /// <typeparam name="T">基类</typeparam>
 /// <param name="model">可以是扩展类对象</param>
 /// <returns></returns>
 public static int delete <T>(T model) where T : class
 {
     using (WeiQingEntities db = new WeiQingEntities())
     {
         var ent = db.Entry(model);
         ent.State = EntityState.Deleted;
         return(db.SaveChanges());
     }
 }
예제 #4
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
        /// <summary>
        /// 查询所有数据, 慎用, 除非可以包装一次取出的数据是有限的, 例如分类名称
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List <T> selectAll <T>() where T : class, new()
        {
            string tableName = typeof(T).Name;

            using (WeiQingEntities db = new WeiQingEntities())
            {
                var set  = db.Set <T>();
                var list = set.SqlQuery(string.Format("select * from `{0}`", tableName)).ToList();
                return(list);
            }
        }
예제 #5
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
        /// <summary>
        /// 使用linq进行简单的泛型查询,一次最多只能得到一条记录
        /// </summary>
        /// <typeparam name="T">需要得到的类型, 查询哪张表(必须和表名称一致)</typeparam>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public static T select <T>(int id) where T : class, new()
        {
            string tableName = typeof(T).Name;

            using (WeiQingEntities db = new WeiQingEntities())
            {
                var set   = db.Set <T>();
                var model = set.SqlQuery(string.Format("select * from `{0}` where id={1}", tableName, id)).FirstOrDefault();
                return(model);
            }
        }
예제 #6
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Delete(object obj)
        {
            int effect = -1;

            using (WeiQingEntities con = new WeiQingEntities())
            {
                con.Database.Log = new Action <string>(q => Debug.WriteLine(q));
                DbEntityEntry entry = con.Entry(obj);
                entry.State = EntityState.Deleted;
                effect      = con.SaveChanges();
                return(effect);
            }
        }
예제 #7
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Insert(object obj)
        {
            Type t      = obj.GetType();
            int  effect = -1;

            using (WeiQingEntities con = new WeiQingEntities())
            {
                con.Database.Log = new Action <string>(q => Debug.WriteLine(q));
                DbSet set = con.Set(t);
                set.Add(obj);
                effect = con.SaveChanges();
                return(effect);
            }
        }
예제 #8
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static int DeleteMany(IEnumerable <object> objs)
        {
            int effect = 0;

            var et = objs.GetEnumerator();

            if (et.MoveNext())
            {
                Type t = et.Current.GetType();
                using (WeiQingEntities con = new WeiQingEntities())
                {
                    con.Database.Log = new Action <string>(q => Debug.WriteLine(q));
                    foreach (var o in objs)
                    {
                        DbEntityEntry entry = con.Entry(o);
                        entry.State = EntityState.Deleted;
                    }
                    effect = con.SaveChanges();
                }
            }
            return(effect);
        }
예제 #9
0
파일: EfExt.cs 프로젝트: tiankaijun/LunTan
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static int InsertMany(IEnumerable <object> objs)
        {
            int effect = 0;

            var et = objs.GetEnumerator();

            if (et.MoveNext())
            {
                Type t = et.Current.GetType();
                using (WeiQingEntities con = new WeiQingEntities())
                {
                    con.Database.Log = new Action <string>(q => Debug.WriteLine(q));
                    DbSet set = con.Set(t);
                    foreach (var o in objs)
                    {
                        set.Add(o);
                    }
                    effect = con.SaveChanges();
                }
            }
            return(effect);
        }