In this section, we will discuss how you can use ObjectServices
to add, modify, and delete an object within an ObjectContext
.
To add an object to the ObjectContext
requires the use of
the AddObject
method. It accepts the entity set name and entity instance as parameters and adds the object passed to it to the ObjectContext
. Once you have added an object to the ObjectContext
, you can call the SaveChanges
to persist the changes to the database.
Here is an example that shows how you can add an object to the ObjectContext
and then call the SaveChanges
method to persist the changes to the database:
PayrollModel.PayrollEntities ctx = new PayrollModel.PayrollEntities(); PayrollModel.Employee employee = new PayrollModel.Employee(); employee.EmployeeID = 16; employee.EmployeeName = "Debanjan Banerjee"; employee.EmployeeAddress = "Kolkata"; employee.JoiningDate = DateTime.Now; employee.Department = ctx.Department. Where(d => d.DepartmentID == 3).First(); ctx...