Пример #1
0
 /// <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());
     }
 }
Пример #2
0
 /// <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());
     }
 }
Пример #3
0
        /// <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);
            }
        }
Пример #4
0
        /// <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);
            }
        }
Пример #5
0
        /// <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);
        }
Пример #6
0
        /// <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);
        }