예제 #1
0
        public void ModifiedStatusExample()
        {
            using (EFCoreContext db = new EFCoreContext())
            {
                Department department = db.Departments.Where(f => f.Name == "Production").FirstOrDefault();
                department.Name = "Production 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();
        }
예제 #2
0
        public void DeletedStatusExample()
        {
            AddDepartment();
            using (EFCoreContext db = new EFCoreContext())
            {
                Department department = db.Departments.Where(f => f.Name == "HR").FirstOrDefault();
                db.Remove(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();
        }
예제 #3
0
        public void DeleteMultipleRecordsDisconnected()
        {
            List <Department> departments = new List <Department>();

            departments.Add(new Department {
                DepartmentID = 1
            });
            departments.Add(new Department {
                DepartmentID = 2
            });

            //Deleting Multiple Records
            using (EFCoreContext db = new EFCoreContext())
            {
                db.Entry(departments).State = EntityState.Deleted;



                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            Console.ReadKey();
        }
예제 #4
0
        private void UpdateDisconnected()
        {
            Department department;

            //Disconnected Scenario
            using (EFCoreContext db = new EFCoreContext())
            {
                department = db.Departments.Where(d => d.Name == "Sales").First();
            }

            department.Descr = "Sales Department-Disconnected Scenario";
            using (EFCoreContext db = new EFCoreContext())
            {
                db.Entry(department).State = EntityState.Modified;

                //OR
                //db.Departments.Attach(department);
                //db.Entry(department).State = System.Data.Entity.EntityState.Modified;

                //OR
                //db.Departments.Add(department);
                //db.Entry(department).State = System.Data.Entity.EntityState.Modified;


                db.SaveChanges();
            }

            Console.WriteLine("Department {0} ({1}) is Updated ", department.Name, department.DepartmentID);
            Console.ReadKey();

            //UPDATE [Departments] SET [Descr] = @p0, [Name] = @p1
            //WHERE [DepartmentID] = @p2;
            //SELECT @@ROWCOUNT;
        }
예제 #5
0
        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();
        }
예제 #6
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();
        }
예제 #7
0
        public void Reload()
        {
            Console.WriteLine("Reload Example ");

            //Adding
            using (EFCoreContext db = new EFCoreContext())
            {
                Department department = db.Departments.Where(f => f.Name == "HR").FirstOrDefault();
                db.Entry(department).Reload();
            }

            Console.WriteLine("Press any key to continue ");
            Console.ReadKey();
        }
예제 #8
0
        private void UpdateWrongWay(int id, string descr)
        {
            Department department = new Department();

            department.DepartmentID = id;
            department.Descr        = descr;

            using (EFCoreContext db = new EFCoreContext())
            {
                db.Entry(department).State = EntityState.Modified;
                db.SaveChanges();
            }

            Console.WriteLine("Department {0} ({1}) is Updated ", department.Name, department.DepartmentID);
            Console.ReadKey();
        }
예제 #9
0
        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);
            }
        }
예제 #10
0
        public void DeleteDisconnectedWithoutLoading()
        {
            Department department;

            department = new Department()
            {
                DepartmentID = 36
            };

            using (EFCoreContext db = new EFCoreContext())
            {
                db.Entry(department).State = EntityState.Deleted;
                db.SaveChanges();
            }
            Console.WriteLine("Department {0} is Deleted ", department.DepartmentID);
            Console.ReadKey();
        }
예제 #11
0
        public void DeleteDisconnected()
        {
            Department department;

            //Disconnected Scenario
            using (EFCoreContext db = new EFCoreContext())
            {
                department = db.Departments.Where(d => d.Name == "Sales").First();
            }


            using (EFCoreContext db = new EFCoreContext())
            {
                //This also works
                //db.Departments.Attach(department);
                //db.Entry(department).State = System.Data.Entity.EntityState.Deleted;

                db.Entry(department).State = EntityState.Deleted;
                db.SaveChanges();
            }

            Console.WriteLine("Department {0} ({1}) is Deleted ", department.Name, department.DepartmentID);
            Console.ReadKey();
        }
예제 #12
0
        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();
        }