Venciity

EF - DAOClass

Mar 7th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace DAO
  8. {
  9.     class DAOClass
  10.     {
  11.         private static SoftUniEntities softUniEntities = new SoftUniEntities();
  12.         static void Main(string[] args)
  13.         {
  14.             //var hireDate = DateTime.Today.AddDays(1);
  15.             //insertEmployee("Ventsislav", "Yordanov", "C# Developer", 1, hireDate, 1250m, null, null, null);
  16.  
  17.             //var hireDate2 = DateTime.Today.AddDays(3);
  18.             //insertEmployee("Alex", "Obretenov", "C# Developer", 1, hireDate2, 1500m, "Ivanov", 288, 200);
  19.  
  20.             DeleteEmployee(298);
  21.  
  22.             UpdateEmployee(1, "FirstName", "Gosho");
  23.         }
  24.  
  25.         public static void insertEmployee(string firstName, string lastName, string jobTitle, int departmentId,
  26.             DateTime hireDate, decimal salary, string middleName = null, int? managerId = null, int? addressId = null)
  27.         {
  28.             var newEmployee = new Employee
  29.             {
  30.                 FirstName = firstName,
  31.                 LastName = lastName,
  32.                 MiddleName = middleName,
  33.                 JobTitle = jobTitle,
  34.                 DepartmentID = departmentId,
  35.                 ManagerID = managerId,
  36.                 HireDate = hireDate,
  37.                 Salary = salary,
  38.                 AddressID = addressId
  39.             };
  40.             softUniEntities.Employees.Add(newEmployee);
  41.             softUniEntities.SaveChanges();
  42.             Console.WriteLine("Successfuly insert employee" + Environment.NewLine +
  43.                               "EmployeeID: " + newEmployee.EmployeeID + Environment.NewLine +
  44.                               "FirstName: " + firstName + Environment.NewLine +
  45.                               "LastName: " + lastName + Environment.NewLine +
  46.                               "MiddleName: " + middleName + Environment.NewLine +
  47.                               "JobTitle: " + jobTitle + Environment.NewLine +
  48.                               "DepartmentID: " + departmentId + Environment.NewLine +
  49.                               "ManagerID: " + managerId + Environment.NewLine +
  50.                               "HireDate: " + hireDate + Environment.NewLine +
  51.                               "Salary: " + salary + Environment.NewLine +
  52.                               "AddressID: " + addressId + Environment.NewLine);
  53.         }
  54.  
  55.         public static void DeleteEmployee(int employeeId)
  56.         {
  57.             Employee employee = softUniEntities.Employees.Find(employeeId);
  58.  
  59.             if (employee != null)
  60.             {
  61.                 softUniEntities.Employees.Remove(employee);
  62.                 softUniEntities.SaveChanges();
  63.                 Console.WriteLine("Successfully deleted employee with id: {0}", employeeId);
  64.             }
  65.         }
  66.  
  67.         public static void UpdateEmployee(int employeeId, string properyName, object newValue)
  68.         {
  69.             Employee employee = softUniEntities.Employees.Find(employeeId);
  70.             var oldValue = employee.GetType().GetProperty(properyName).GetValue(employee);
  71.  
  72.             if (employee != null)
  73.             {
  74.                 employee.GetType().GetProperty(properyName).SetValue(employee, newValue);
  75.                 softUniEntities.SaveChanges();
  76.                 Console.WriteLine("Successfully updated propery: {0} on employee with Id: {1}." + Environment.NewLine +
  77.                                   "Old value: {2}, New Value: {3}",
  78.                                   properyName, employeeId, oldValue, newValue);
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment