15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

862 ❘ ChaPTer 31 AdO.net entity frAmewOrk<br />

the Orders rows for a customer, you need to do a SQL join statement. With object-oriented languages, it<br />

is more common to define a Customer <strong>and</strong> an Order class <strong>and</strong> access the orders of a customer by using an<br />

Orders property from the Customer class.<br />

For object-relational mapping since .<strong>NET</strong> 1.0, it was possible to use the DataSet class <strong>and</strong> typed datasets.<br />

Datasets are very similar to the structure of a database containing DataTable, DataRow, DataColumn, <strong>and</strong><br />

DataRelation classes instead of offering object-support. The ADO.<strong>NET</strong> Entity Framework gives support<br />

to directly define entity classes that are completely independent of a database structure <strong>and</strong> map them to<br />

tables <strong>and</strong> associations of the database. Using objects with the application, the application is shielded from<br />

changes in the database.<br />

The ADO.<strong>NET</strong> Entity Framework makes use of Entity SQL to define entity-based queries to the store. LINQ<br />

to Entities makes it possible to use the LINQ syntax to query data. An object context keeps knowledge about<br />

entities that are changed, to have information when the entities should be written back to the store.<br />

The namespaces that contain classes from the ADO.<strong>NET</strong> Entity Framework are listed in the following table.<br />

namesPaCe<br />

System.Data<br />

System.Data.Common<br />

System.Data.Common.Comm<strong>and</strong>Trees<br />

System.Data.Entity.Design<br />

System.Data.EntityClient<br />

System.Data.Objects<br />

System.Data.Objects.DataClasses<br />

desCriPTion<br />

This is a main namespace for ADO.<strong>NET</strong>. With the ADO.<strong>NET</strong><br />

Entity Framework. This namespace contains exception classes<br />

related to entities — for example MappingException <strong>and</strong><br />

QueryException.<br />

This namespace contains classes shared by .<strong>NET</strong> data providers.<br />

The class DbProviderServices is an abstract base class that<br />

must be implemented by an ADO.<strong>NET</strong> Entity Framework provider.<br />

This namespace contains classes to build an expression tree.<br />

This namespace contains classes used by the designer to create<br />

Entity Data Model (EDM) files.<br />

This namespace specifies classes for the .<strong>NET</strong> Framework Data<br />

Provider to access the Entity Framework. EntityConnection,<br />

EntityComm<strong>and</strong>, <strong>and</strong> EntityDataReader can be used to<br />

access the Entity Framework.<br />

This namespace contains classes to query <strong>and</strong> update databases.<br />

The class ObjectContext encapsulates the connection to the<br />

database <strong>and</strong> serves as a gateway for create, read, update, <strong>and</strong><br />

delete methods. The class ObjectQuery represents a query<br />

against the store. CompiledQuery is a cached query.<br />

This namespace contains classes <strong>and</strong> interfaces required<br />

for entities.<br />

enTiTy frameWorK maPPing<br />

The ADO.<strong>NET</strong> Entity Framework offers several layers to map database tables to objects. You can start<br />

with a database schema <strong>and</strong> use a Visual Studio item template to create the complete mapping. You can also<br />

start designing entity classes with the designer <strong>and</strong> map it to the database where the tables <strong>and</strong> associations<br />

between the tables can have a very different structure.<br />

The layers that need to be defined are as follows:<br />

➤<br />

➤<br />

➤<br />

Logical — This layer defines the relational data.<br />

Conceptual — This layer defines the .<strong>NET</strong> classes.<br />

Mapping — This layer defines the mapping from .<strong>NET</strong> classes to relational tables <strong>and</strong> associations.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!