Querying Against the Entity Data Model

The real power of EF is the ability to perform SQL-like operations against a set of objects (for example, the objects you have defined in your entity data model) using a variety of different methods.

Because every object in EF is LINQ enabled, we could use LINQ to Entities to query for a list of salaried employees, like this.

using (AdventureWorksEntities entities = new AdventureWorksEntities())
   {
      List<Employee> employees =
          (from e in entities.Employees
           where e.SalariedFlag == true select e).ToList();
   }

The AdventureWorksEntities object seen on the first line of code above represents our conceptual model; LINQ provides the syntax we need to iterate over those objects within the AdventureWorksEntities object.

EF also supports the ability to construct queries using the ObjectQuery class. This class lives within the System.Data.Objects namespace and enables us to use standard SQL-like syntax for generating queries. Here is that same query, rewritten using the ObjectQuery class.

using (AdventureWorksEntities entities = new AdventureWorksEntities())
   {
      ObjectQuery<Employee> query =
         entities.Employees.Where("it.SalariesFlag=@flag");

         query.Parameters.Add(new ObjectParameter("flag", "True"));

         List<Employee> employees = query.ToList();

   }

We could even query our objects using nothing more than standard stored procedures. This would be a two-step process. First, we create a stored procedure in our underlying database (and thus our store model). Second, we map that stored procedure to a function within our model. With the function in place (here we have named it GetSalariedEmployees), we can call that function like this.

using (AdventureWorksEntities entities = new AdventureWorksEntities())
   {
      List<Employee> employees = entities.GetSalariedEmployees().ToList();
   }

Updating Data in the Entity Data Model

Because we are simply programming against standard .NET objects within the entity data model, updating data that resides in those objects is as simple as setting the object properties.

We can create a new employee like this.

Employee newEmp = new Employee();

We can set its properties like we would for any other object.

newEmp.SalariedFlag = true;
newEmp.HireDate = DateTime.Today;
newEmp.Title = "HR Manager";

We then add the new employee to the Entity Data Model like this.

entities.Employees.AddObject(newEmp);

Finally, we would need to tell the Entity Data Model to persist these changes to the database.

entities.SaveChanges();

In a similar fashion, we can delete an object by first referencing it and then calling the DeleteObject method from our entities object. This code deletes the first employee in the Employee table.

Employee firstEmp = entities.Employees.First();
entities.DeleteObject(firstEmp);


Note

We have discussed how to change data within the Entity Data Model, but what happens if either the database schema changes or you want changes within your conceptual model to be made at the storage level as well? If you right-click the Entity Data Model designer, you see two options that essentially sync schema of the database with the object model or vice versa: Generate Database from Model and UpdateModelFromDatabase.


As you have probably guessed by now, Entity Framework is a vast and deep ORM platform. We have really only been able to scratch the surface here. If you intend to leverage EF inside your applications, we wholeheartedly recommend a book dedicated to the subject, such as Julia Lerman’s Programming Entity Framework from O’Reilly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset