Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Working with Object Context in the ADO.NET Entity Framework : Page 2

Take advantage of the Object Context in Entity Framework to perform CRUD operations and attach or detach entities.


advertisement

Working with the Object Context

The Object Context in Entity Framework (like as in LINQ to SQL) is the gateway to execute your queries against the Entity Data Model. Any object that is returned as a result of a query execution is attached to the Object Context. The Object Context can in turn track changes to the object and also persist the object to the data store.

The Object Context interacts with the database and abstracts the way the connection string, the connection to the underlying database, the queries, and the stored procedures are executed. It also manages reads and writes to and from the database.



The Object Context uses the ObjectStateManager to manage the state changes of objects and then applies those changes to the underlying data store appropriately. The Object Context in Entity Framework is represented by the class called ObjectContext.

CRUD Operations Using the ObjectContext

You can use the Object Context to perform CRUD operations on your data exposed by the Entity Data Model. Here's how:

  1. Create the record

    Let's create an employee record, as an example:

    PayrollEntities dataContext = new PayrollEntities(); try { Employee emp = new Employee { EmployeeID = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Kolkata" }; dataContext.Employees.AddObject(emp); dataContext.SaveChanges(); } catch { //Write your code here to handle errors. }

  2. Update record

    To modify the employee record, you can use the following code:

    PayrollEntities dataContext = new PayrollEntities(); try { Employee emp = dataContext.Employees.First(e=>e.EmployeeID == 1); emp.Address = "Hyderabad"; dataContext.SaveChanges(); } catch { //Write your code here to handle errors. }

  3. Delete record

    To delete the employee record, you can use the following code:

    PayrollEntities dataContext = new PayrollEntities(); try { Employee emp = dataContext.Employees.First(e=>e.EmployeeID == 1); dataContext.DeleteObject(emp); dataContext.SaveChanges(); } catch { //Write your code here to handle errors. }

In the next section, you will explore how to query data exposed by the Entity Data Model using Object Context.

Querying the Entity Data Model Using Object Context

The following code snippet illustrate how you can use your Object Context instance to query data exposed by the Entity Data Model.

using (var dataContext = new NorthwindEntities()) { var Customers = from c in dataContext.Customer select c; foreach (var Customer in Customers) { Console.WriteLine(String.Format("{0} {1}", Customer.Name, Customer.Address )); } }

Attaching and Detaching Objects from the Object Context

You can attach or detach objects to and from the Object Context using methods such as, Attach() or Detach(). To attach a previously detached object to the Object Context, you can use the following code:

using (NorthwindEntities dataContext = new NorthwindEntities()) { dataContext.Attach(employeeObj); }

To detach objects from the ObjectContext you can use the System.Data.Objects.ObjectSet.Detach() method or the System.Data.Objects.ObjectContext.Detach(System.Object) method.

You can also specify the entity to be detached as follows:

dataContext.Entry(entity).State = EntityState.Detached;

To check if an entity is already detached, you can use this code.

if (entity == null || entity.EntityState != EntityState.Detached) Console.WriteLine("Entity is null or already detached.");



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap