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.

entity Client ❘ 867<br />

EntityDataReader reader = comm<strong>and</strong>.ExecuteReader(<br />

Comm<strong>and</strong>Behavior.CloseConnection | Comm<strong>and</strong>Behavior.SequentialAccess);<br />

while (reader.Read())<br />

{<br />

Console.WriteLine("{0}, {1}", reader["Title"], reader["Publisher"]);<br />

}<br />

reader.Close();<br />

code snippet BooksDemo/Program.cs<br />

The code is done in the same way as in Chapter 30. The only real difference is the connection string <strong>and</strong> the<br />

Entity SQL statements; that ’ s why we look at these now.<br />

Connection string<br />

With the earlier code snippet, the connection string is read from the confi guration fi le. The connection<br />

string is required for EDM <strong>and</strong> is different from the normal ADO.<strong>NET</strong> connection string because mapping<br />

information is required. The mapping is defi ned with the keyword metadata . metadata requires three<br />

things: a delimited list of mapping fi les, Provider for the invariant provider name to access the data source,<br />

<strong>and</strong> a Provider connection string to assign the provider - dependent connection string.<br />

The delimited list of mapping fi les references the fi les BooksModel.csdl , BooksModel.ssdl , <strong>and</strong><br />

BooksModel.msl , which are contained within resources in the assembly as defi ned with the res: prefi x.<br />

Within Visual Studio, the designer uses just one fi le, BooksModel.edmx , which contains CSDL, SSDL,<br />

<strong>and</strong> MSL. Setting the property Custom Tool to EntityModelCodeGenerator creates three fi les that are<br />

contained within resources.<br />

Within the connectionString setting you can fi nd the connection string to the database with the<br />

connection string setting. This part is the same as a simple ADO.<strong>NET</strong> connection string <strong>and</strong> depends on the<br />

provider that is set with the provider setting:<br />

<br />

<br />

<br />

With the connection string you can also specify CSDL, SSDL, <strong>and</strong> MSL fi les that are<br />

not contained as a resource in the assembly. This is useful if you want to change the<br />

content of these fi les after deployment of the project.<br />

entity sql<br />

For querying data with the Entity Client, Entity SQL is used. Entity SQL enhances T - SQL by adding types.<br />

This syntax doesn ’ t require joins because associations of entities can be used instead.<br />

Here, just a few syntax options are shown that help you start with Entity SQL. In the MSDN<br />

documentation you can fi nd the complete reference.<br />

The previous example showed how Entity SQL uses defi nitions from the CSDL in the EntityContainer<br />

<strong>and</strong> EntitySet — for example, BooksEntities.Books to get all the books from the table Books because<br />

the Books EntitySet maps to the Books EntitySet in the SSDL.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!