03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

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

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

504 x CHAPTER 14 LINQ AND THE ADO.<strong>NET</strong> ENTITY FRAMEWORK<br />

The page looks rather messy because of the way the data is presented <strong>in</strong> the GridView, but <strong>in</strong> later exercises<br />

you see how to improve the layout of the grid <strong>and</strong> the data.<br />

FIGURE 14-4<br />

How It Works<br />

EF comes with an object-relational designer (accessible <strong>in</strong> VS) that enables you to create an object<br />

model that is accessible through code based on the tables <strong>in</strong> your database. By add<strong>in</strong>g tables to this<br />

designer, VS generates code for you that enables you to access the underly<strong>in</strong>g data <strong>in</strong> the database without<br />

writ<strong>in</strong>g a lot of code. The classes that are added to the designer are stored <strong>in</strong> the .edmx file <strong>and</strong> its<br />

Code Beh<strong>in</strong>d files. The designer file (the Code Beh<strong>in</strong>d of the PlanetWrox.edmx file) conta<strong>in</strong>s a class<br />

that <strong>in</strong>herits from ObjectContext, the ma<strong>in</strong> object <strong>in</strong> EF that provides access to your database. In the<br />

preced<strong>in</strong>g exercise, this class is called PlanetWroxEntities (named after the .edmx file) <strong>and</strong> you use it<br />

to access the data <strong>in</strong> the tables you added to the diagram. Although you normally don’t need to look at<br />

the generated code, you can open the file PlanetWrox.designer.vb or PlanetWrox.designer.cs file<br />

<strong>and</strong> see what code has been generated for you. The *.tt files you deleted enable you to customize the<br />

code that is generated based on the underly<strong>in</strong>g data model. However, <strong>in</strong> most cases you don’t have to<br />

do this, <strong>and</strong> you can rely on the default code generation <strong>in</strong>stead. That’s why you deleted these files, <strong>and</strong><br />

configured the EF model to use its default code generation strategy.<br />

The designer is smart enough to detect the relationships <strong>in</strong> the database <strong>and</strong> is thus able to create the<br />

necessary relationships <strong>in</strong> code as well, as you saw <strong>in</strong> Figure 14-2. The model def<strong>in</strong>es two ma<strong>in</strong> object<br />

types, Review <strong>and</strong> Genre, both of which also have collection counterparts called Reviews <strong>and</strong> Genres,<br />

respectively. These collections are referred to as entity sets. Note that on English versions of VS the<br />

designer has correctly pluralized the names of the Review <strong>and</strong> Genre tables (Reviews <strong>and</strong> Genres, respectively),<br />

mak<strong>in</strong>g it easier to see what is a collection (Reviews) <strong>and</strong> what is a s<strong>in</strong>gle <strong>in</strong>stance of an object<br />

(Review). For other language versions of VS, you had to apply this logic yourself us<strong>in</strong>g the Entity Designer.<br />

After the model has been generated, you can execute LINQ queries aga<strong>in</strong>st it to get data out of the<br />

underly<strong>in</strong>g database. To access the data, you need an <strong>in</strong>stance of the ObjectContext class, which is<br />

created <strong>in</strong>side the Us<strong>in</strong>g block <strong>in</strong> the code. A Us<strong>in</strong>g block (us<strong>in</strong>g <strong>in</strong> C#) is used to wrap code that creates<br />

a variable that must be disposed of (cleared from memory) as soon as you’re done with it. Because

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

Saved successfully!

Ooh no, something went wrong!