Esempio n. 1
0
 List <T> IDAL <T> .GetModelList(Expression <Func <T, bool> > whereLambda)
 {
     using (testEntities db = new testEntities())
     {
         var dbQuery = db.Set <T>();
         if (whereLambda == null)
         {
             return(db.Set <T>().ToList());
         }
         return(dbQuery.Where(whereLambda).ToList());
     }
 }
Esempio n. 2
0
        public int UpdateData(test data, testEntities entity)
        {
            int success = 0;
            //entity需一樣,所以移除
            //testEntities entity = (testEntities)base.GetCurrentContext();
            var entry = entity.Entry(data);

            if (entry.State == EntityState.Detached)
            {
                var  set            = entity.Set <test>();
                test attachedEntity = set.Find(data.id);

                if (attachedEntity != null)
                {
                    var attachedEntry = entity.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(data);
                }
                else
                {
                    entry.State = EntityState.Modified;
                }
            }

            success = entity.SaveChanges();
            return(success);
        }
Esempio n. 3
0
 int IDAL <T> .Add(T model)
 {
     using (testEntities db = new testEntities())
     {
         db.Set <T>().Add(model);
         return(db.SaveChanges());
     }
 }
Esempio n. 4
0
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static bool UpdateEntity <T>(T entity, params string[] properties) where T : class
        {
            var ent = db.Entry(entity);

            db.Set <T>().Attach(entity);
            if (properties.Length > 0)
            {
                foreach (var pro in properties)
                {
                    ent.Property(pro).IsModified = true;
                }
            }
            else
            {
                db.Entry <T>(entity).State = EntityState.Modified;
            }
            return(db.SaveChanges() > 0);
        }
Esempio n. 5
0
 int IDAL <T> .Delete(Expression <Func <T, bool> > whereLambda)
 {
     using (testEntities db = new testEntities())
     {
         var dbQuery = db.Set <T>();
         var list    = dbQuery.Where(whereLambda).ToList();
         foreach (var item in list)
         {
             dbQuery.Remove(item);
         }
         return(db.SaveChanges());
     }
 }
Esempio n. 6
0
 int IDAL <T> .Update(Expression <Func <T, bool> > whereLambda, string[] propertyNames, object[] propertyValues)
 {
     using (testEntities db = new testEntities())
     {
         var  list = db.Set <T>().Where <T>(whereLambda).ToList();
         Type t    = typeof(T);
         foreach (var item in list)
         {
             for (int index = 0; index < propertyNames.Length; index++)
             {
                 string       name = propertyNames[index];
                 PropertyInfo pi   = t.GetProperty(name);
                 pi.SetValue(item, propertyValues[index], null);
             }
         }
         return(db.SaveChanges());
     }
 }
Esempio n. 7
0
 public Repository(testEntities dbEntity)
 {
     this.dbEntites = dbEntity;
     dbSet          = dbEntites.Set <T>();
 }
Esempio n. 8
0
 public Repository(testEntities context)
 {
     this.context = context;
     this.dbSet   = context.Set <T>();
 }