public bool insert(Products entity) { using (var db = new ProductWithQRCodeDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { Products _pro = db.Products.OrderByDescending(u => u.ID).FirstOrDefault(); if (_pro != null) { entity.ID = _pro.ID + 1; } else { entity.ID = 1; } db.Products.Add(entity); db.SaveChanges(); dbContextTransaction.Commit(); return(true); } catch (Exception) { dbContextTransaction.Rollback(); return(false); } } } }
public bool update(Products entity) { using (var db = new ProductWithQRCodeDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try{ db.Products.Attach(entity); db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); dbContextTransaction.Commit(); return(true); } catch (Exception) { dbContextTransaction.Rollback(); return(false); } } } }
public bool Insert(Categories entity) { using (var db = new ProductWithQRCodeDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { db.Categories.Add(entity); db.SaveChanges(); dbContextTransaction.Commit(); return(true); } catch (Exception) { dbContextTransaction.Rollback(); return(false); } } } }
public bool UpdateMultiRecords(int[] idList) { using (var db = new ProductWithQRCodeDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { var categories = db.Categories.Where(x => idList.Contains(x.ID)).ToList(); categories.ForEach(a => a.Status = false); db.SaveChanges(); dbContextTransaction.Commit(); return(true); } catch (Exception) { dbContextTransaction.Rollback(); return(false); } } } }
public bool delete(int _proId) { using (var db = new ProductWithQRCodeDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { var product = db.Products.SingleOrDefault(x => x.ID == _proId); product.Status = false; db.Products.Attach(product); db.Entry(product).State = EntityState.Modified; db.SaveChanges(); dbContextTransaction.Commit(); return(true); } catch (Exception) { dbContextTransaction.Rollback(); return(false); } } } }
public int Delete(int _cateId) { using (var db = new ProductWithQRCodeDbContext()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { var category = db.Categories.Find(_cateId); category.Status = false; db.Categories.Attach(category); db.Entry(category).State = EntityState.Modified; db.SaveChanges(); dbContextTransaction.Commit(); return((int)category.Type); } catch (Exception) { dbContextTransaction.Rollback(); return(0); } } } }