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(); }
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; }
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(); }
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(); } }
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(); }
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(); } }
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(); }
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(); } }
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(); }
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; }
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(); } }
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(); } }
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(); }
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(); }
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(); }
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(); }
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(); }
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 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(); }
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(); }
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(); }
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(); } }
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(); }
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(); } }
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(); }
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(); }