<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codelab Blog &#187; ADO.NET</title>
	<atom:link href="http://blog.codelab.co.nz/tag/ado-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codelab.co.nz</link>
	<description>Technical Articles and News from Codelab Ltd</description>
	<lastBuildDate>Tue, 17 Jan 2012 13:10:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>EF Strongly Typed ObjectQuery.Include</title>
		<link>http://blog.codelab.co.nz/2010/07/13/ef-strongly-typed-objectquery-include/</link>
		<comments>http://blog.codelab.co.nz/2010/07/13/ef-strongly-typed-objectquery-include/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 20:37:58 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ADO.NET]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=274</guid>
		<description><![CDATA[The problem that I faced was having to put up with &#8220;hard coded&#8221; strings in the ObjectQuery.Include function to load related objects through POCO objects in the Entity Framework. This faced challenges when I renamed columns in the Model which caused a run-time error when recompiling and re-starting the application. Thanks to David Kiff, we [...]]]></description>
			<content:encoded><![CDATA[<p>The problem that I faced was having to put up with &#8220;hard coded&#8221; strings in the ObjectQuery.Include function to load related objects through POCO objects in the Entity Framework.   This faced challenges when I renamed columns in the Model which caused a run-time error when recompiling and re-starting the application.   Thanks to <a href="http://davidkiff.co.uk/author/David+Kiff.aspx">David Kiff</a>, we can now change the code from being strings:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">using (CommuicationsEntities entities = new CommuicationsEntities())<br />
{<br />
entities.Order.Include(&quot;OrderDetails&quot;);<br />
}</div></div>
<p>To a nice strongly typed includes using a IncludeBuilder class.</p>
<div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public class IncludeBuilder<br />
{<br />
private readonly List _propertiesToInclude;<br />
<br />
public IncludeBuilder()<br />
{<br />
_propertiesToInclude = new List();<br />
}<br />
<br />
public void Add(Expression&lt;Func&lt;T, object&gt;&gt; propertySelector)<br />
{<br />
MemberExpression memberExpression = propertySelector.Body as MemberExpression;<br />
if (memberExpression == null)<br />
throw new ArgumentException(&quot;Parameter propertySelector must be a MemberExpression&quot;);<br />
_propertiesToInclude.Add(memberExpression.Member.Name);<br />
}<br />
<br />
public ObjectQuery Includes(ObjectQuery query)<br />
{<br />
foreach (string include in _propertiesToInclude)<br />
{<br />
query = query.Include(include);<br />
}<br />
return query;<br />
}<br />
}</div></div>
<p>the following code calls the include builder, includes the relationship and gets a simple list with no tracking on the POCO objects.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IncludeBuilder _include = new IncludeBuilder();<br />
_include.Add(a =&gt; a.Orders);<br />
ObjectQuery customers= DataContext.Servers;<br />
return _include.Includes(customers).Execute(MergeOption.NoTracking).ToList();</div></div>
<p><em><strong>References:</strong></em></p>
<p><a href="http://davidkiff.co.uk/post/2009/08/27/EF-Strongly-Typed-ObjectQuery3cT3eInclude%28e2809ce2809d%293b.aspx">http://davidkiff.co.uk/post/2009/08/27/EF-Strongly-Typed-ObjectQuery3cT3eInclude%28e2809ce2809d%293b.aspx</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2010/07/13/ef-strongly-typed-objectquery-include/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

