예제 #1
0
        private void UpdateMultipleRecords()
        {
            List <Department> departments = new List <Department>();

            departments.Add(new Department {
                DepartmentID = 1, Descr = "Sales"
            });
            departments.Add(new Department {
                DepartmentID = 2, Descr = "Purchase"
            });
            departments.Add(new Department {
                DepartmentID = 3, Descr = "HR"
            });

            using (EFCoreContext db = new EFCoreContext())
            {
                foreach (var item in departments)
                {
                    var dept = db.Departments.Where(f => f.DepartmentID == item.DepartmentID).FirstOrDefault();
                    if (dept == null)
                    {
                        throw new Exception("");
                    }

                    dept.Descr = item.Descr;
                }

                db.SaveChanges();
            }

            Console.WriteLine("Records Updated ");
            Console.ReadKey();
        }
예제 #2
0
        private void UpdateConnected()
        {
            Department department;

            //Connected Scenario
            using (EFCoreContext db = new EFCoreContext())
            {
                department       = db.Departments.Where(d => d.Name == "Sales").First();
                department.Descr = "This is Sales Department";
                db.SaveChanges();

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

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


            //SELECT TOP(1) [d].[DepartmentID], [d].[Descr], [d].[Name]
            //FROM [Departments] AS [d]
            //WHERE [d].[Name] = N'Sales'

            //UPDATE [Departments] SET [Descr] = @p0
            //WHERE [DepartmentID] = @p1;

            //SELECT @@ROWCOUNT;
        }
예제 #3
0
        public void AddListOfOjects()
        {
            using (EFCoreContext db = new EFCoreContext())
            {
                List <Department> deps = new List <Department>();
                deps.Add(new Department {
                    Name = "Dept1", Descr = ""
                });
                deps.Add(new Department {
                    Name = "Dept2", Descr = ""
                });
                db.Departments.AddRange(deps);
                db.SaveChanges();

                Console.WriteLine("{0} Departments added ", deps.Count);
                Console.ReadKey();
            }


            //INSERT INTO [Departments] ([Descr], [Name])
            //VALUES (@p0, @p1);

            //SELECT [DepartmentID]
            //FROM [Departments]
            //WHERE @@ROWCOUNT = 1 AND [DepartmentID] = scope_identity();

            //INSERT INTO [Departments] ([Descr], [Name])
            //VALUES (@p0, @p1);

            //SELECT [DepartmentID]
            //FROM [Departments]
            //WHERE @@ROWCOUNT = 1 AND [DepartmentID] = scope_identity();
        }
예제 #4
0
        public void AddListOfOjects1()
        {
            //This wont add
            using (EFCoreContext db = new EFCoreContext())
            {
                List <Department> deps = db.Departments.ToList();

                //These records are not added
                deps.Add(new Department {
                    Name = "Dept3", Descr = ""
                });
                deps.Add(new Department {
                    Name = "Dept4", Descr = ""
                });

                //This record is added
                db.Departments.Add(new Department {
                    Name = "Dept5", Descr = ""
                });


                db.SaveChanges();

                Console.WriteLine("Departments added");
                Console.ReadKey();
            }
        }
예제 #5
0
        public void AddSingleRecord()
        {
            using (EFCoreContext db = new EFCoreContext())
            {
                Department department = new Department();
                department.Name = "Secuirty";
                db.Departments.Add(department);

                db.SaveChanges();

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

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



            //INSERT INTO [Departments] ([Descr], [Name])
            //VALUES (@p0, @p1);

            //SELECT [DepartmentID]
            //FROM [Departments]
            //WHERE @@ROWCOUNT = 1 AND [DepartmentID] = scope_identity();
        }
예제 #6
0
 public void insertDepartments()
 {
     using (EFCoreContext db = new EFCoreContext())
     {
         List <Department> departments = new List <Department>();
         departments.Add(new Department {
             Name = "HR", Descr = ""
         });
         departments.Add(new Department {
             Name = "Finance", Descr = ""
         });
         departments.Add(new Department {
             Name = "Accounts", Descr = ""
         });
         departments.Add(new Department {
             Name = "Production", Descr = ""
         });
         departments.Add(new Department {
             Name = "Marketing", Descr = ""
         });
         departments.Add(new Department {
             Name = "Maintenance", Descr = ""
         });
         db.Departments.AddRange(departments);
         db.SaveChanges();
     }
 }
예제 #7
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();
        }
예제 #8
0
        public void AddRelatedData2()
        {
            // Adding Department & Employee
            using (EFCoreContext db = new EFCoreContext())
            {
                // Creat a new department
                Department department = new Department();
                department.Name      = "Bowling";
                department.Employees = new List <Employee>();
                department.Employees.Add(new Employee {
                    FirstName = "Anil", LastName = "Kumble"
                });
                department.Employees.Add(new Employee {
                    FirstName = "Harbajan", LastName = "Singh"
                });

                //Add department to Departments
                //Note that we are not adding Employee. Employee is already added to Dep
                db.Departments.Add(department);

                //Save
                db.SaveChanges();

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

            //This wont add
            using (EFCoreContext db = new EFCoreContext())
            {
                Department department = new Department();
                department = new Department {
                    DepartmentID = 10, Name = "Sales", Descr = ""
                };

                db.Departments.Add(department);

                try
                {
                    db.Database.OpenConnection();
                    db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT Departments ON");
                    db.SaveChanges();
                    db.Database.ExecuteSqlRaw("SET IDENTITY_INSERT Departments OFF;");
                }
                catch (Exception ex)
                {
                    throw;
                } finally
                {
                    db.Database.CloseConnection();
                }


                Console.WriteLine("Departments added ");
                Console.ReadKey();
            }
        }
예제 #12
0
        static void deleteAll()
        {
            using (EFCoreContext db = new EFCoreContext())
            {
                db.RemoveRange(db.Departments.ToList());
                db.RemoveRange(db.Employees.ToList());
                db.RemoveRange(db.Regions.ToList());
                db.RemoveRange(db.Territories.ToList());
                db.RemoveRange(db.SalesPersons.ToList());

                db.SaveChanges();
            }
        }
예제 #13
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();
        }
예제 #14
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();
        }
예제 #15
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();
        }
예제 #16
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();
        }
예제 #17
0
        private void UpdateRightWay(int id, string descr)
        {
            using (EFCoreContext db = new EFCoreContext())
            {
                Department department = db.Departments.Where(f => f.DepartmentID == id).FirstOrDefault();
                if (department == null)
                {
                    throw new Exception("");
                }

                department.Descr = descr;
                db.SaveChanges();
            }

            Console.WriteLine("Records Updated");
            Console.ReadKey();
        }
예제 #18
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);
            }
        }
예제 #19
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();
        }
예제 #20
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();
        }
예제 #21
0
        public void DeleteConnected()
        {
            Department department;

            //Connected Scenario
            using (EFCoreContext db = new EFCoreContext())
            {
                department = db.Departments.Where(d => d.Name == "Sales").FirstOrDefault();
                db.Departments.Remove(department);
                db.SaveChanges();

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

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
예제 #22
0
        public void DeleteMultipleRecordsConnected()
        {
            //Deleting Multiple Records
            using (EFCoreContext db = new EFCoreContext())
            {
                List <Department> deps = db.Departments.Take(2).ToList();
                db.Departments.RemoveRange(deps);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            Console.ReadKey();
        }
예제 #23
0
        public void AddRelatedData4()
        {
            // Adding Department & Employee
            using (EFCoreContext db = new EFCoreContext())
            {
                //Add Employee with Deparmtnet
                Employee emp = new Employee();
                emp.FirstName    = "Kapil";
                emp.LastName     = "Dev";
                emp.DepartmentID = 10;
                db.Employees.Add(emp);

                //Save
                db.SaveChanges();

                //Console.WriteLine("Employee {0} ({1}) is added in the department {2} ", emp.FirstName, emp.EmployeeID, emp.Department.Name);
                Console.ReadKey();
            }
        }
예제 #24
0
        public void AddRelatedData1()
        {
            // Adding Department & Employee
            using (EFCoreContext db = new EFCoreContext())
            {
                //Create new employee
                Employee emp = new Employee();
                emp.FirstName = "Anil";
                emp.LastName  = "Kumble";


                // Creat a new department
                Department department = new Department();
                department.Name      = "Bowling";
                department.Employees = new List <Employee>();
                department.Employees.Add(emp);

                //Add department to Departments
                //Note that we are not adding Employee. Employee is already added to Dep
                db.Departments.Add(department);

                //Save
                db.SaveChanges();

                Console.WriteLine("Department {0} ({1}) is added ", department.Name, department.DepartmentID);
                Console.WriteLine("Employee {0} ({1}) is added in the department {2} ", emp.FirstName, emp.EmployeeID, emp.Department.Name);
                Console.ReadKey();
            }


            //INSERT INTO[Departments] ([Descr], [Name])
            //VALUES(@p0, @p1);
            //SELECT[DepartmentID]
            //FROM[Departments]
            //WHERE @@ROWCOUNT = 1 AND[DepartmentID] = scope_identity();
            //INSERT INTO[Employees] ([DepartmentID], [FirstName], [LastName])
            //VALUES(@p2, @p3, @p4);
            //SELECT[EmployeeID]
            //FROM[Employees]
            //WHERE @@ROWCOUNT = 1 AND[EmployeeID] = scope_identity();
        }
예제 #25
0
        public void AddRelatedData3()
        {
            // Adding Department & Employee
            using (EFCoreContext db = new EFCoreContext())
            {
                // Get the existing Department
                Department department = db.Departments.Where(f => f.Name == "Bowling").FirstOrDefault();

                //Add Employee with Deparmtnet
                Employee emp = new Employee();
                emp.FirstName  = "Kapil";
                emp.LastName   = "Dev";
                emp.Department = department;
                db.Employees.Add(emp);

                //Save
                db.SaveChanges();

                Console.WriteLine("Department {0} ({1}) is added ", department.Name, department.DepartmentID);
                Console.WriteLine("Employee {0} ({1}) is added in the department {2} ", emp.FirstName, emp.EmployeeID, emp.Department.Name);
                Console.ReadKey();
            }
        }
예제 #26
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();
        }
예제 #27
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();
        }