querying entity data model using objectcontext
The ObjectContext is the main class in the Entity Framework which interacts with the database. The ObjectContext maintains the connection to the database and also manages all the read and write operations.
Entity Data Model can be queried using Linq to Entities or by Entity Sql or by Linq methods. Whichever the query method, results are always returned as objects. All the objects returned by a query are attached to an ObjectContext.
Using the ObjectStateManager, the ObjectContext tracks the changes to the objects and applies the changes to the data store. The ObjectStateManager holds the references of all the objects. When a transaction occurs, the ObjectContext keeps track of the order of operations and generates SQL to execute the operations efficiently.
When we query an object with the ObjectContext, information about the object is stored in the ObjectContext state manager. And, when we call SaveChanges() method of the ObjectContext, the ObjectContext looks for all the objects in the ObjectStateManager and does appropriate actions on each object.
Querying EDM using ObjectContext
In the code given below, we retrieve all the customers using LINQ to Entities query using c#.
class Program { static void Main(string[] args) { using (var context = new SalesOrderManagementEntities()) { var Customers = from c in context.Customer select c; foreach (var Customer in Customers) { Console.WriteLine(String.Format("{0} {1}", Customer.Name, Customer.Address )); } Console.WriteLine(); Console.ReadLine(); } } }
The SalesOrderManagementEntities is a class inherited from the ObjectContext. SalesOrderManagementEntities represents EntityContainer.
SalesOrderManagementEntities class has the property customer which is a EntitySet in the Entity Data Model.
In the LINQ query given above, we have used ObjectContext class's property Customer to get data from the Customer table. The result of above query are objects and are automatically attached to the ObjectContext.
Attaching and detaching objects from the ObjectContext
Attaching and detaching can be done either by calling methods such as add, attach, or detach or using the functionality of entity framework. The below code demonstrates how to attach an object to the ObjectContext.
using (var context = new SalesOrderManagementEntities()) { Customer customerdelete = new Customer { CustomerId = 1 }; context.AttachTo("Customer", customerdelete); context.DeleteObject(customerdelete); context.SaveChanges(); }
Entity Framework Articles
- entity framework entity data model
- entity framework orm object relational mapper
- using ado net entity data model in windows applications.html
- create entity data model using ado.net entity framework designer in visual studio
- create-sales-order-management-data-model-using-edmgen-tool.html
- display data in a gridview using ado.net entity data source control
- ways of querying entity data model
- query entity data model using linq to entities
- query entity data model with object services and entity sql
- query entity data model using linq methods
- parameterized objectquery against a entity data model
- query entity data model entity client
- linq to entities query with projections in c#
- entity framework - save changes to entities
- entity framework - insert new objects
- add an entity to associated entities
- deleting entities using deleteobject() method of objectcontext
- entity framework load method
- eager loading using include method in entity framework
- entity framework - retrieving a single entity
Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net