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.

object Context ❘ 871<br />

Class or inTerfaCe<br />

IEntityWithKey<br />

desCriPTion<br />

This interface defi nes an EntityKey property that allows fast access to<br />

the object.<br />

IEntityWithChangeTracker This interface defi nes the method SetChangeTracker() where a change<br />

tracker that implements the interface IChangeTracker can be assigned to<br />

get information about state change from the object.<br />

IEntityWithRelationships This interface defi nes the read-only property RelationshipManager,<br />

which returns a RelationshipManager object that can be used to navigate<br />

between objects.<br />

For an entity class, it ’ s not necessary to derive from the base classes EntityObject or<br />

ComplexObject . Instead, an entity class can implement the required interfaces.<br />

The Book entity class can easily be accessed by using the object context class BooksEntities . The Books<br />

property returns a collection of Book objects that can be iterated:<br />

using (var data = new BooksEntities())<br />

{<br />

foreach (var book in data.Books)<br />

{<br />

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

}<br />

}<br />

code snippet BooksDemo/Program.cs<br />

objeCT ConTeXT<br />

To retrieve data from the database, the ObjectContext class is needed. This class defi nes the mapping from<br />

the entity objects to the database. With core ADO.<strong>NET</strong>, you can compare this class to the data adapter that<br />

fi l l s a DataSet .<br />

The BooksEntities class created by the designer derives from the base class ObjectContext . This class<br />

adds constructors to pass a connection string. With the default constructor, the connection string is read<br />

from the confi guration fi le. It is also possible to pass an already opened connection to the constructor in the<br />

form of an EntityConnection instance. If you pass a connection to the constructor that is not opened,<br />

the object context opens <strong>and</strong> closes the connection; if you pass an opened connection you also need to close it.<br />

The created class defi nes Books <strong>and</strong> Authors properties, which return an ObjectSet < TEntity > .<br />

ObjectSet < TEntity > that is new with .<strong>NET</strong> 4 <strong>and</strong> derives from ObjectQuery < TEntity > :<br />

public partial class BooksEntities : ObjectContext<br />

{<br />

public BooksEntities() : base("name=BooksEntities", "BooksEntities")<br />

{<br />

this.ContextOptions.LazyLoadingEnabled = true;<br />

OnContextCreated();<br />

}<br />

public BooksEntities(string connectionString) : base(connectionString, "BooksEntities")<br />

{<br />

this.ContextOptions.LazyLoadingEnabled = true;<br />

OnContextCreated();<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!