예제 #1
0
 public virtual void Delete(T data)
 {
     using (messageEntities db = new messageEntities())
     {
         Type           type    = data.GetType();
         PropertyInfo[] members = type.GetProperties();
         List <string>  ids     = new List <string>();
         foreach (PropertyInfo m in members)
         {
             var temp = m.GetCustomAttribute <KeyAttribute>();
             if (temp != null)
             {
                 object obj = m.GetValue(data);
                 ids.Add(obj.ToString());
                 continue;
             }
         }
         T entity = db.Set <T>().Find(ids.ToArray());
         if (entity != default(T))
         {
             db.Set <T>().Remove(entity);
             db.SaveChanges();
         }
     }
 }
예제 #2
0
        public virtual void DeleteRange(IList <T> datas)
        {
            using (messageEntities db = new messageEntities())
            {
                IList <T> list = new List <T>();
                foreach (T data in datas)
                {
                    Type           type    = data.GetType();
                    PropertyInfo[] members = type.GetProperties();
                    List <string>  ids     = new List <string>();
                    foreach (PropertyInfo m in members)
                    {
                        var temp = m.GetCustomAttribute <KeyAttribute>();
                        if (temp != null)
                        {
                            object obj = m.GetValue(data);
                            ids.Add(obj.ToString());
                            continue;
                        }
                    }
                    T entity = db.Set <T>().Find(ids.ToArray());
                    list.Add(entity);
                }

                if (list.Count > 0)
                {
                    foreach (var l in list)
                    {
                        db.Set <T>().Remove(l);
                    }

                    db.SaveChanges();
                }
            }
        }
예제 #3
0
 public virtual IList <T> FindAll()
 {
     using (messageEntities entity = new messageEntities())
     {
         return(entity.Set <T>().ToList());
     }
 }
예제 #4
0
 public virtual T Find(string id)
 {
     using (messageEntities db = new messageEntities())
     {
         T entity = db.Set <T>().Find(id);
         return(entity);
     }
 }
예제 #5
0
 public virtual T Add(T data)
 {
     using (messageEntities entity = new messageEntities())
     {
         entity.Set <T>().Add(data);
         try
         {
             entity.SaveChanges();
         }
         catch (System.Data.Entity.Validation.DbEntityValidationException ex)
         {
             throw ex;
         }
         return(data);
     }
 }
예제 #6
0
        public virtual void AddRange(IList <T> datas)
        {
            using (messageEntities entity = new messageEntities())
            {
                foreach (var data in datas)
                {
                    entity.Set <T>().Add(data);
                }

                try
                {
                    entity.SaveChanges();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                {
                    throw ex;
                }
            }
        }