Example #1
0
 static void QueryItems(Context context)
 {
     // context.Departments.Load();
     var employees = context.Employees.ToList();
     Console.WriteLine("All emp");
     foreach(var e in employees)
         Console.WriteLine("{0} - {1}", e.Name, e.Department != null ? e.Department.Name : "None");
 }
Example #2
0
 static void RemoveItem(Context context)
 {
     var employee = context.Employees.FirstOrDefault(e => e.Name.StartsWith("A"));
     if (employee != null)
     {
         context.Employees.Remove(employee);
         context.SaveChanges();
     }
 }
Example #3
0
        static void AddItems(Context context)
        {
            var departmentSD = new Department { Name = "Software development" };
            var departmentHR = new Department { Name = "HR" };
            context.Departments.Add(departmentSD);
            context.Departments.Add(departmentHR);

            context.Employees.Add(new Employee { Name = "Sergey Efremov", Department = departmentSD });
            context.Employees.Add(new Employee { Name = "Andrey Ivanov", Department = departmentHR });
            
            context.SaveChanges();
        }
Example #4
0
        static void Main(string[] args)
        {   
            // Change
            // Before running the program, check the database name in App.config
            using (var context = new Context()) // Context class is disposable
            {                
                // Uncomment the following line to print requests sent to the database
                // context.Database.Log += Console.WriteLine;               

                // Uncomment one item at a time
                // AddItems(context);
                // EditItem(context);
                // RemoveItem(context);
                
                QueryItems(context);
            }            
        }
Example #5
0
        static void Query2(Context context)
        {

        }
Example #6
0
 static void EditItem(Context context)
 {
     var employee = context.Employees.First(e => e.Name.StartsWith("A"));
     employee.Name = "Sergey";            
     context.SaveChanges();
 }