public void AddDepartment() { using (EFCoreContext db = new EFCoreContext()) { Department department = new Department(); department.Name = "Secuirty"; db.Entry(department).State = EntityState.Added; department = new Department(); department.Name = "HR"; db.Add(department); db.SaveChanges(); Console.WriteLine("Department {0} ({1}) is added ", department.Name, department.DepartmentID); } }
public void AddStatusExample() { using (EFCoreContext db = new EFCoreContext()) { Department department = new Department(); department.Name = "Production"; db.Add(department); Console.WriteLine("Status Before SaveChanges " + db.Entry(department).State.ToString()); db.SaveChanges(); Console.WriteLine("Status After SaveChanges " + db.Entry(department).State.ToString()); } Console.WriteLine("Press any key to continue "); Console.ReadKey(); }
public void CheckStatus() { Console.WriteLine("Checking Status"); using (EFCoreContext db = new EFCoreContext()) { Department department = db.Departments.Where(f => f.Name == "HR").FirstOrDefault(); db.Entry(department).State.ToString(); Console.WriteLine("Status " + db.Entry(department).State.ToString()); department = new Department(); db.Entry(department).State.ToString(); Console.WriteLine("Status " + db.Entry(department).State.ToString()); db.Add(department); db.Entry(department).State.ToString(); Console.WriteLine("Status " + db.Entry(department).State.ToString()); } Console.WriteLine("Press any key to continue "); Console.ReadKey(); }