Ejemplo n.º 1
0
        public static void Main()
        {
            var context = new SoftUniEntities();

            var newEmployee = new Employee
            {
                FirstName = "Ivan",
                LastName = "Ivanov",
                JobTitle = "Database administrator",
                DepartmentID = context.Departments
                                    .Where(d => d.Name == "Research and Development")
                                    .Select(a => a.DepartmentID)
                                    .First(),
                HireDate = DateTime.Now,
                Salary = 15000
            };

            Console.WriteLine("1. Add new employee");
            Console.WriteLine("   -- {0} with Id: {1}", DataAccessObject.Add(newEmployee), newEmployee.EmployeeID);

            Console.WriteLine("2. Find employee by key");
            var employee = DataAccessObject.FindByKey(newEmployee.EmployeeID);
            Console.WriteLine("   -- {0} {1}", employee.FirstName, employee.LastName);

            Console.WriteLine("3. Edit employee first name");
            employee.FirstName = "Todor";
            Console.WriteLine("   -- " + DataAccessObject.Modify(employee));
            Console.WriteLine("   -- New name: {0} {1}", employee.FirstName, employee.LastName);

            Console.WriteLine("4. Delete employee");
            Console.WriteLine("   -- {0}", DataAccessObject.Delete(employee));
        }
 public static void Delete(Employee employee)
 {
     var context = new SoftUniEntities();
     var deletedEmp = context.Employees.Remove(employee);
     var name = deletedEmp.FirstName == "Guy";
     context.SaveChanges();
     Console.WriteLine("The deleted employee's name is {0}", name);
 }
 public static void Modify(Employee employee)
 {
     var context = new SoftUniEntities();
     var foundEmp = context.Employees.Find(3);
     foundEmp.FirstName = "Vyara";
     foundEmp.LastName = "Georgieva";
     context.SaveChanges();
     Console.WriteLine("The employee's with Id {0} name now is {1} {2}",foundEmp.EmployeeID, foundEmp.FirstName, foundEmp.LastName);
 }
Ejemplo n.º 4
0
        public static string Add(Employee employee)
        {
            using (var context = new SoftUniEntities())
            {
                context.Employees.Add(employee);
                context.SaveChanges();

            return "Employee added";
            }
        }
Ejemplo n.º 5
0
        public static string Modify(Employee employee)
        {
            using (var context = new SoftUniEntities())
            {
                var original = context.Employees.Find(employee.EmployeeID);
                context.Entry(original).CurrentValues.SetValues(employee);
                context.SaveChanges();

                return "Employee modified";
            }
        }
 public static void Add(Employee employee)
 {
     var context = new SoftUniEntities();
     context.Employees.Add(employee);
     employee.FirstName = "Peter";
     employee.LastName = "Petrov";
     employee.JobTitle = "Senior JS";
     employee.DepartmentID = 2;
     employee.HireDate = DateTime.Now;
     employee.Salary = 60000;
     context.SaveChanges();
     Console.WriteLine("Employee added: First Name: {0}, Last Name {1}", employee.FirstName, employee.LastName);
 }
Ejemplo n.º 7
0
 static void Main(string[] args)
 {
     var context = new SoftUniEntities();
     Employee newEmployee = new Employee
     {
         FirstName = "Goshko",
         LastName = "Dendimonov",
         JobTitle = "Gamer",
         DepartmentID = 2,
         HireDate = DateTime.Now,
         Salary = 10000
     };
     DAOClass.Add(newEmployee);
     var primaryKey = newEmployee.EmployeeID;
     Console.WriteLine("{0}'s ID Key is {1}", newEmployee.FirstName, primaryKey);
     DAOClass.Modify(newEmployee, "Toshko");
     Console.WriteLine("{0}'s ID Key is {1}", newEmployee.FirstName, primaryKey);
     DAOClass.Delete(newEmployee);
 }
Ejemplo n.º 8
0
        public static string Delete(Employee employee)
        {
           using (var context = new SoftUniEntities())
            {
                //var itemToRemove = context.Employees.SingleOrDefault(e => e.EmployeeID == employee.EmployeeID);

                context.Entry(employee).State = EntityState.Deleted;
                context.SaveChanges();
//
//                if (itemToRemove != null)
//                {
//                    context.Employees.Remove(itemToRemove);
//                    context.SaveChanges();

                    return "Employee deleted";
                //}

                return "Delete failed!";  
            }
        }