public static Employee FindByKey(object key) { var context = new SoftUniEntities(); var employee = context.Employees .Find(key); return employee; }
public static void Add(Employee employee) { var context = new SoftUniEntities(); context.Employees.Add(employee); context.SaveChanges(); Console.WriteLine("Employee has been added."); }
public static void Modify(int employeeId, string property, object value) { var context = new SoftUniEntities(); var selectedEmployee = context.Employees.Find(employeeId); selectedEmployee.GetType().GetProperty(property).SetValue(selectedEmployee, value); context.SaveChanges(); Console.WriteLine("Employee has been modified."); }
public static void Delete(int employeeId) { var context = new SoftUniEntities(); var selectedEmployee = context.Employees.Find(employeeId); context.Employees.Remove(selectedEmployee); context.SaveChanges(); Console.WriteLine("Employee has been deleted."); }
//LINQ query: public static void PintNamesWithLinqQuery() { var context = new SoftUniEntities(); var employees = context.Employees .Where(e => e.Projects.Any(p => p.StartDate.Year == 2002)) .Select(e => e.FirstName); //foreach (var employee in employees) //{ // Console.WriteLine(employee); //} }
static void Main() { var context = new SoftUniEntities(); //call the stored procedure (see it in SoftUniEntities.Context.cs) //to find all projects for given employee by his/her firstname and lastname var projects = context.GetProjectsByEmployee("Ruth", "Ellerbrock"); foreach (var project in projects) { Console.WriteLine("{0} - {1} - {2}", project.Name, project.Description, project.StartDate); } }
//Find all employees who have projects with start date in the year 2002. Select only their first name. //native SQL query: public static void PintNamesWithNativeQuery() { var context = new SoftUniEntities(); string query = @"SELECT e.[FirstName] FROM [SoftUni].[dbo].[Employees] e JOIN EmployeesProjects ep ON ep.EmployeeID = e.EmployeeID JOIN Projects p ON ep.ProjectID = p.ProjectID WHERE DATEPART(yy, p.StartDate) = 2002"; var employees = context.Database.SqlQuery<string>(String.Format(query)).ToList(); //foreach (var employee in employees) //{ // Console.WriteLine(employee); //} }
static void Main() { var contextOne = new SoftUniEntities(); var empOne = contextOne.Employees.Find(1); empOne.FirstName = "Mincho"; var contextTwo = new SoftUniEntities(); var empTwo = contextTwo.Employees.Find(1); empTwo.FirstName = "Zdravko"; contextOne.SaveChanges(); contextTwo.SaveChanges(); //result with [Concurrency Mode] = None: second win: the first name is set to Zdravko //result with [Concurrency Mode] = Fixed: first win: the first name is set to Mincho }
static void Main() { //Measure the difference in performance in both cases with a Stopwatch. var context = new SoftUniEntities(); //Establish connection to server in advance var totalCount = context.Employees.Count(); var sw = new Stopwatch(); sw.Start(); PintNamesWithNativeQuery(); Console.WriteLine("Native: {0}", sw.Elapsed); sw.Restart(); PintNamesWithLinqQuery(); Console.WriteLine("Linq: {0}", sw.Elapsed); //result: Native: 00:00:00.0503817; Linq: 00:00:00.0081855 }