Exemple #1
0
        public void AttachExample()
        {
            Console.WriteLine("Attach Example");

            Department department1 = new Department();

            department1.Name = "Production";

            Department department2 = new Department();

            department2.DepartmentID = 10;
            department2.Name         = "Finance";


            using (EFCoreContext db = new EFCoreContext())
            {
                Console.WriteLine("Status Before Attach department1 " + db.Entry(department1).State.ToString());   //Detached
                Console.WriteLine("Status Before Attach department2 " + db.Entry(department2).State.ToString());   //Detached

                db.Attach(department1);
                db.Attach(department2);

                Console.WriteLine("Status After Attach department1 " + db.Entry(department1).State.ToString());    //Added
                Console.WriteLine("Status After Attach department2 " + db.Entry(department1).State.ToString());    //Unchanged

                db.Entry(department1).State = EntityState.Detached;
                Console.WriteLine("Status After Detach department1 " + db.Entry(department1).State.ToString());    //Added
            }

            Console.WriteLine("Press any key to continue ");
            Console.ReadKey();
        }