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"); }
static void RemoveItem(Context context) { var employee = context.Employees.FirstOrDefault(e => e.Name.StartsWith("A")); if (employee != null) { context.Employees.Remove(employee); context.SaveChanges(); } }
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(); }
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); } }
static void Query2(Context context) { }
static void EditItem(Context context) { var employee = context.Employees.First(e => e.Name.StartsWith("A")); employee.Name = "Sergey"; context.SaveChanges(); }