////[RequiredHttpsAttribute] ////[BasicAuthenticationAttribute] public IEnumerable <Employee> AllEmployees() { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { return(dbContext.Employees.ToList()); } }
public async Task <Employee> GetEmployeeById(int employeeId) { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { return(await dbContext.Employees.FirstAsync(e => e.Id == employeeId)); } }
//Get the employees for the department selected public ActionResult Index(int departmentId) { EmployeeDBContext dbContext = new EmployeeDBContext(); var employees = dbContext.Employees.Where(emp => emp.Department_Id == departmentId).ToList(); return(View(employees)); }
public Employee Get(string email) { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { return(dbContext.Employees.FirstOrDefault(e => e.Email == email)); } }
// PUT api/values/5 public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (EmployeeDBContext context = new EmployeeDBContext()) { Employee emp = context.Employees.Where(x => x.ID == id).FirstOrDefault(); if (emp == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with " + id + " not found")); } emp.FirstName = employee.FirstName; emp.LastName = employee.LastName; emp.Gender = employee.Gender; emp.Salary = employee.Salary; context.SaveChanges(); HttpResponseMessage message = Request.CreateResponse(HttpStatusCode.OK, employee); message.Headers.Location = new Uri(Request.RequestUri + id.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
public List <Employee> GetAllEmployees() { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { return(dbContext.Employees.ToList()); } }
public virtual void Insert(T obj) { try { if (obj == null) { throw new ArgumentNullException("obj"); } Entities.Add(obj); if (Context == null || _isDisposed) { Context = new EmployeeDBContext(); } //Context.SaveChanges(); commented out call to savechanges as context save changes will be //called with unit of work. } catch (DbEntityValidationException ex) { foreach (var validationErrors in ex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { _errorMessage += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine; } } throw new Exception(_errorMessage, ex); } }
public HttpResponseMessage Delete(int id) { try { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { var entity = dbContext.Employees.FirstOrDefault(e => e.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found to delete")); } else { dbContext.Employees.Remove(entity); dbContext.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public IHttpActionResult Get(int id) { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { var entity = dbContext.Employees.FirstOrDefault(e => e.ID == id); if (entity != null) { return(Ok(entity)); } else { //return NotFound(); //throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with ID " + id.ToString() + "not found")); //see https://www.infoworld.com/article/2994111/how-to-handle-errors-in-aspnet-web-api.html return(ResponseMessage( Request.CreateResponse( HttpStatusCode.NotFound, "Employee with ID " + id.ToString() + " not found" ) )); //see https://stackoverflow.com/questions/20139621/how-do-i-return-notfound-ihttpactionresult-with-an-error-message-or-exception } } }
public virtual void Delete(T entity) { try { if (entity == null) { throw new ArgumentNullException("entity"); } if (Context == null || _isDisposed) { Context = new EmployeeDBContext(); } Entities.Remove(entity); //Context.SaveChanges(); commented out call to SaveChanges as Context save changes will be called with Unit of work } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { _errorMessage += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } throw new Exception(_errorMessage, dbEx); } }
public ActionResult DeleteEmployee(int id) { EmployeeDBContext EC = new EmployeeDBContext(); Employee EmpToDelete = EC.Employees.First(x => x.Id == id); return(View(EmpToDelete)); }
public ActionResult Index() { EmployeeDBContext EC = new EmployeeDBContext(); var EmployeeList = EC.Employees.OrderBy(x => x.LastName).ToList(); return(View(EmployeeList)); }
public JsonResult Post(Department dep) { try { using (EmployeeDBContext db = new EmployeeDBContext()) { Department dep1 = new Department(); if (dep.DepartmentName != "") { dep1.DepartmentName = dep.DepartmentName; } else { return(new JsonResult("Enter name of the department!")); } db.Department.Add(dep1); db.SaveChanges(); } return(new JsonResult("Added Successfully!")); } catch (Exception) { return(new JsonResult("Failed to Add!")); } }
static void Main(string[] args) { using (var context = new EmployeeDBContext()) { var result = context.Set<Employee>().Include(x => x.Department).ToArray(); } }
public Employee Get(int id) { using (EmployeeDBContext employeeDBContext = new EmployeeDBContext()) { return(employeeDBContext.Employees.FirstOrDefault(e => e.ID == id)); } }
public List <DepartmentViewModel> GetDepartmentList() { EmployeeDBContext empDBContext = new EmployeeDBContext(); List <DepartmentViewModel> department = empDBContext.Departments.ToList(); return(department); }
public IEnumerable <Employee> Get() { using (EmployeeDBContext employeeDBContext = new EmployeeDBContext()) { return(employeeDBContext.Employees.ToList()); } }
// To get the departments to view the employees associated with them public ActionResult Index() { EmployeeDBContext dbContext = new EmployeeDBContext(); List <Department> listDepartments = dbContext.Departments.ToList(); return(View(listDepartments)); }
public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { var entity = dbContext.Employees.FirstOrDefault(e => e.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id " + id.ToString() + " not found to update")); } else { entity.FirstName = employee.FirstName; entity.LastName = employee.LastName; entity.Gender = employee.Gender; entity.Salary = employee.Salary; dbContext.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
private bool Login(string userName, string password) { using (EmployeeDBContext dBContext = new EmployeeDBContext()) { return(dBContext.Users.Any(user => user.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase) && user.Password == password)); } }
public IQueryable <Employee> Get() { using (EmployeeDBContext ent = new EmployeeDBContext()) { return(ent.Employees); } }
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log) { log.Info("C# HTTP trigger function processed a request."); //TblEmployee tbl = new TblEmployee(); var datacon = new EmployeeDBContext(); var a = datacon.TblEmployee.Where(b => b.Name == "chandra").Single(); //(from e in datacon.TblEmployee where e.name = "tck" select employee).firstordefaul; var c = datacon.TblEmployee.ToList().GroupBy(p => p.Name); string name = a.Name.ToString() + req.Query["name"]; string requestBody = new StreamReader(req.Body).ReadToEnd(); dynamic data = JsonConvert.DeserializeObject(requestBody); name = name ?? data?.name; return(name != null ? (ActionResult) new OkObjectResult($"Hello Mr, {name}") : new BadRequestObjectResult("Please pass a name on the query string or in the request body")); }
public List <Employee> GetEmployee() { EmployeeDBContext e = new EmployeeDBContext(); List <Employee> data = new List <Employee>(); data = e.Employees.ToList(); return(data); }
public ActionResult DetailsEmployee(int id) { EmployeeDBContext EC = new EmployeeDBContext(); Employee EmpDetails = EC.Employees.First(x => x.Id == id); return(View(EmpDetails)); }
public HttpResponseMessage Get() { using (EmployeeDBContext dbContext = new EmployeeDBContext()) { var Employees = dbContext.Employees.ToList(); return(Request.CreateResponse(HttpStatusCode.OK, Employees)); } }
// GET api/values public IEnumerable <Employee> Get() { using (EmployeeDBContext context = new EmployeeDBContext()) { List <Employee> employees = context.Employees.ToList(); return(employees); } }
protected void Page_Load(object sender, EventArgs e) { EmployeeDBContext employeeDBContext = new EmployeeDBContext(); employeeDBContext.Database.CommandTimeout = 540; GridView1.DataSource = employeeDBContext.employees.ToList(); GridView1.DataBind(); }
public IActionResult GetAllDepartmentNames() { using (EmployeeDBContext db = new EmployeeDBContext()) { var depNames = db.Department.ToList(); return(Json(depNames)); } }
/// <summary> /// Constructor /// </summary> /// <param name="context">Employee DbContext reference</param> /// <param name="cache">Memory Cache</param> /// <param name="logger">Logger</param> public EmployeeRepo(EmployeeDBContext context, IMemoryCache cache, ILogger <EmployeeRepo> logger) { _context = context; _cache = cache; _logger = logger; }
static void Main(string[] args) { Program pg = new Program(); EmployeeDBContext employeeDBContext = new EmployeeDBContext(); pg.PrintEmployees(employeeDBContext.Employees.ToList()); pg.PrintDepartment(employeeDBContext.Department.ToList()); Console.ReadLine(); }