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.

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

public BooksEntities(EntityConnection connection) : base(connection, "BooksEntities")<br />

{<br />

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

OnContextCreated();<br />

}<br />

partial void OnContextCreated();<br />

public ObjectSet Authors<br />

{<br />

get<br />

{<br />

if ((_Authors == null))<br />

{<br />

_Authors = base.CreateObjectSet("Authors");<br />

}<br />

return _Authors;<br />

}<br />

}<br />

private ObjectSet _Authors;<br />

}<br />

public ObjectSet Books<br />

{<br />

get<br />

{<br />

if ((_Books == null))<br />

{<br />

_Books = base.CreateObjectSet("Books");<br />

}<br />

return _Books;<br />

}<br />

}<br />

private ObjectSet _Books;<br />

code snippet BooksDemo/BooksModel.Designer.cs<br />

The ObjectContext class provides several services to the caller:<br />

➤ It keeps track of entity objects that are already retrieved. If the object is queried again, it is taken from<br />

the object context.<br />

➤ It keeps state information about the entities. You can get information about added, modified, <strong>and</strong><br />

deleted objects.<br />

➤ You can update the entities from the object context to write the changes to the underlying store.<br />

Methods <strong>and</strong> properties of the ObjectContext class are listed in the following table.<br />

objeCTConTeXT meThods<br />

<strong>and</strong> ProPerTies<br />

Connection<br />

MetadataWorkspace<br />

QueryTimeout<br />

ObjectStateManager<br />

desCriPTion<br />

Returns a DbConnection object that is associated with the<br />

object context.<br />

Returns a MetadataWorkspace object that can be used to read the<br />

metadata <strong>and</strong> mapping information.<br />

With this property you can get <strong>and</strong> set the timeout value for the<br />

queries of the object context.<br />

This property returns an ObjectStateManager. The<br />

ObjectStateManager keeps track of entity objects retrieved <strong>and</strong><br />

object changes in the object context.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!