public User GetByIdUsingDbContext(int id)
 {
     using (var db = new AppEntities())
     {
         return(db.Users.Find(id));
     }
 }
 public void Update(User entity)
 {
     using (var db = new AppEntities())
     {
         db.Entry(entity).State = EntityState.Modified;
         db.SaveChanges();
     }
 }
 public void Add(User entity)
 {
     using (var db = new AppEntities())
     {
         db.Users.Add(entity);
         db.SaveChanges();
     }
 }
 public IEnumerable <User> GetAll()
 {
     using (var db = new AppEntities())
     {
         //Don't forget ToList or this will throw InvalidOperationException for disposing DbContext
         return(db.Users.ToList());
     }
 }
 public void Delete(int id)
 {
     using (var db = new AppEntities())
     {
         User userToDelete = db.Users.Find(id);
         if (userToDelete != null)
         {
             db.Users.Remove(userToDelete);
             db.SaveChanges();
         }
     }
 }