Пример #1
0
        public IList <T> GetAll(int startRow, int maxRow, params Expression <Func <T, object> >[] navigationProperties)
        {
            try
            {
                using (var db = new DBEntities.DBEntities())
                {
                    List <T>       list;
                    IQueryable <T> dbQuery = db.Set <T>();

                    //Apply eager loading
                    foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                    {
                        dbQuery = dbQuery.Include <T, object>(navigationProperty);
                    }

                    list = dbQuery
                           .AsNoTracking()
                           .ToList <T>();
                    this.TotalRows = list.Count();
                    return(list.Skip(startRow).Take(maxRow).ToList());
                }
            }
            catch
            {
                throw;
            }
        }
Пример #2
0
 /// <summary>
 /// Delete multi items
 /// </summary>
 /// <param name="items">List items</param>
 /// <returns>True/False</returns>
 public bool Delete(List <T> items)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             DbSet <T> dbSet;
             dbSet = db.Set <T>();
             dbSet.RemoveRange(items);
             db.SaveChanges();
             return(true);
         }
     }
     catch (DbUpdateException ex)
     {
         var sqlException = ex.GetBaseException() as SqlException;
         // Exception occur when having other table relation with this table
         if (sqlException != null && sqlException.Number == 547)
         {
             this.AddMessage("DEL-000002", ex.Message);
             return(false);
         }
         else
         {
             throw;
         }
     }
     catch
     {
         throw;
     }
 }
Пример #3
0
        /// <summary>
        /// Get single object
        /// </summary>
        /// <param name="where">Where some conditions</param>
        /// <param name="navigationProperties"></param>
        /// <returns>Return Object </returns>
        public T GetSingle(Func <T, bool> where, params Expression <Func <T, object> >[] navigationProperties)
        {
            try
            {
                using (var db = new DBEntities.DBEntities())
                {
                    T item = null;
                    IQueryable <T> dbQuery = db.Set <T>();

                    //Apply eager loading
                    foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                    {
                        dbQuery = dbQuery.Include <T, object>(navigationProperty);
                    }

                    item = dbQuery
                           .AsNoTracking()         //Don't track any changes for the selected item
                           .FirstOrDefault(where); //Apply where clause
                    this.TotalRows = 1;
                    return(item);
                }
            }
            catch
            {
                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// Get list object by some conditions
        /// </summary>
        /// <param name="where">Where conditions</param>
        /// <param name="navigationProperties"></param>
        /// <returns></returns>
        public IList <T> GetList(Func <T, bool> where, params Expression <Func <T, object> >[] navigationProperties)
        {
            try
            {
                using (var db = new DBEntities.DBEntities())
                {
                    List <T>       list;
                    IQueryable <T> dbQuery = db.Set <T>();

                    //Apply eager loading
                    foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                    {
                        dbQuery = dbQuery.Include <T, object>(navigationProperty);
                    }

                    list = dbQuery
                           .AsNoTracking()
                           .Where(where)
                           .ToList <T>();
                    this.TotalRows = list.Count();
                    return(list);
                }
            }
            catch
            {
                throw;
            }
        }
Пример #5
0
 /// <summary>
 /// Get current message
 /// </summary>
 /// <returns>Return message</returns>
 public string GetMessage()
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             return(db.Message.Find(MsgCode).Messages);
         }
     }
     catch
     {
         return(null);
     }
 }
Пример #6
0
 /// <summary>
 /// Get message error by msgCode
 /// </summary>
 /// <param name="Code">Message Code</param>
 /// <returns>Return message</returns>
 public string GetMessage(string Code, string Lang)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             return(db.Message.Find(Code, Lang).Messages);
         }
     }
     catch
     {
         return(null);
     }
 }
Пример #7
0
 /// <summary>
 /// Get object by object ID
 /// </summary>
 /// <param name="ID">object ID</param>
 /// <returns>Return only object</returns>
 public T GetByID(object ID)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             return(db.Set <T>().Find(ID));
         }
     }
     catch
     {
         throw;
     }
 }
Пример #8
0
 /// <summary>
 /// Add new item
 /// </summary>
 /// <param name="item">Object</param>
 /// <returns>True/False</returns>
 public bool Add(T item)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             db.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Added;
             db.SaveChanges();
             return(true);
         }
     }
     catch
     {
         throw;
     }
 }
Пример #9
0
 private RefNo GetRefNoConfig(int storeID, string refType)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             var rowRefNo = (from c in db.RefNo
                             where c.StoreId == storeID && c.RefType == refType
                             select c).FirstOrDefault();
             return(rowRefNo);
         }
     }
     catch
     {
         return(null);
     }
 }
Пример #10
0
 public bool GetOptionSystemByValue(int storeID, string OptionID)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             var select = from c in db.OptionSystem
                          where c.StoreId == storeID && c.OptionName == OptionID
                          select c;
             return(select.First().Value == "1" ? true : false);
         }
     }
     catch
     {
         return(false);
     }
 }
Пример #11
0
 public string GetOptionSystemByID(int storeID, string OptionID)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             var select = from c in db.OptionSystem
                          where c.StoreId == storeID && c.OptionName == OptionID
                          select c;
             return(select.First().Value);
         }
     }
     catch
     {
         return(null);
     }
 }
Пример #12
0
 public Fiscal GetCurrentFiscal()
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             var select = from c in db.Fiscal
                          where c.IsCurrent == true
                          select c;
             return(select.FirstOrDefault());
         }
     }
     catch
     {
         return(null);
     }
 }
Пример #13
0
 /// <summary>
 /// Update item
 /// </summary>
 /// <param name="item">Item</param>
 /// <returns>True/False</returns>
 public bool Update(T item)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             Microsoft.EntityFrameworkCore.DbSet <T> dbSet = db.Set <T>();
             db.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
             db.SaveChanges();
             return(true);
         }
     }
     catch
     {
         throw;
     }
 }
Пример #14
0
 /// <summary>
 /// Update list items
 /// </summary>
 /// <param name="items">List items</param>
 /// <returns>True/False</returns>
 public bool Update(List <T> items)
 {
     try
     {
         using (var db = new DBEntities.DBEntities())
         {
             foreach (T item in items)
             {
                 db.Entry(item).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
             }
             db.SaveChanges();
             return(true);
         }
     }
     catch
     {
         return(false);
     }
 }
Пример #15
0
        /// <summary>
        /// Delete item by ID
        /// </summary>
        /// <param name="ID">Item ID</param>
        /// <returns>True/False</returns>
        public bool Delete(object ID)
        {
            try
            {
                using (var db = new DBEntities.DBEntities())
                {
                    DbSet <T> dbSet;
                    dbSet = db.Set <T>();
                    T obj = db.Set <T>().Find(ID);
                    if (db.Entry(obj).State == EntityState.Detached)
                    {
                        dbSet.Attach(obj);
                    }

                    dbSet.Remove(obj);
                    db.SaveChanges();
                    return(true);
                }
            }
            catch (DbUpdateException ex)
            {
                var sqlException = ex.GetBaseException() as SqlException;
                // Exception occur when having other table relation with this table
                if (sqlException != null && sqlException.Number == 547)
                {
                    this.AddMessage("DEL-000002", ex.Message);
                    return(false);
                }
                else
                {
                    throw;
                }
            }
            catch
            {
                this.AddMessage("DEL-000001", "Delete dữ liệu không thành công");
                throw;
            }
        }