<?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>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Entity Framework</title>
	<atom:link href="http://elegantcode.com/category/entity-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Ultram For Sale</title>
		<link>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate</link>
		<comments>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:12:26 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4786</guid>
		<description><![CDATA[Ultram For Sale, A common complaint of the Entity Framework is slow insert times for larger datasets. Ultram overnight, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this post a while back from Mikael Eliasson [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Ultram For Sale</b>, A common complaint of the Entity Framework is slow insert times for larger datasets.  <b>Ultram overnight</b>, Last night I was trying to insert a catalog of 15k products and it was taking a very long time (I gave up after 5 minutes). I recalled this <a href="http://mikee.se/Archive.aspx/Details/using_the_sqlbulkcopy_to_batch_inserts_20111129">post</a> a while back from Mikael Eliasson demonstrating SqlBulkCopy using .NET, <b>Ultram pharmacy</b>.  <b>Ultram maximum dosage</b>, I had used BCP in SQL server, but not from .NET, <b>Ultram reviews</b>.  <b>Ultram treatment</b>, I took Mikael’s example and roughed out a reusable generic version below, which produced 15k inserts in 2.4s or +- 6200 rows per second, <b>cheap Ultram</b>.  I upped it to 4 catalogs, 224392 rows in 39s, for +- 5750 rps (changing between 4 files), <b>Ultram For Sale</b>.  <b>Buying Ultram online over the counter</b>, These are pretty decent records too, 41 columns and a few of the fields have a meaty char count, <b>Ultram dosage</b>.  <b>Ultram results</b>, Good enough I say.</p>  <p><code>[gist]http://gist.github.com/1681480[/gist]</code></p>  <p><code><span style="font-family: verdana">This works off just a basic list of items which property names match the table column names. Given that most POCO based ORM’s generally match the schema exactly, <b>purchase Ultram for sale</b>, <b>Ultram duration</b>, it works great with EF code first objects.</span></code></p>  <p><code><span style="font-family: verdana">To use I just build up a list of objects, pick the connection string off the DbContext and then call BulkInsert to save to the DB, <b>comprar en línea Ultram, comprar Ultram baratos</b>.  <b>Is Ultram safe</b>, Note that in this case I am just adding items to a List&lt;T&gt;, not the EF DbSet&lt;T&gt;.</span></code></p>  <p><code>[gist]http://gist.github.com/1681888[/gist]</code></p>  <p><code><span style="font-family: verdana"></span></code></p>, <b>Ultram overnight</b>.  Ultram class.  Ultram recreational.  Order Ultram online c.o.d.  Ultram from canadian pharmacy.  Where can i cheapest Ultram online.  Purchase Ultram online.  Order Ultram from mexican pharmacy.  Doses Ultram work.  Is Ultram addictive.  Buy no prescription Ultram online.  Online buy Ultram without a prescription.  My Ultram experience.  Where can i find Ultram online.  Ultram price.  Ultram mg.  Ordering Ultram online.  Ultram images.  Ultram price, coupon.  Fast shipping Ultram.  Real brand Ultram online.  Ultram schedule.  Low dose Ultram.  Buy cheap Ultram no rx.  Purchase Ultram online no prescription.  Ultram pictures.  Discount Ultram.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4721'>Buy Flonase Without Prescription</a>. <a href='http://elegantcode.com/?p=4234'>Buy Periactin Without Prescription</a>. <a href='http://elegantcode.com/?p=4203'>Buy Armour Without Prescription</a>. <a href='http://elegantcode.com/?p=4899'>Buy Macrobid Without Prescription</a>. <a href='http://elegantcode.com/?p=4534'>Xalatan For Sale</a>. <a href='http://elegantcode.com/?p=4302'>Phenergan wiki</a>. <a href='http://elegantcode.com/?p=4268'>Soma maximum dosage</a>. <a href='http://elegantcode.com/?p=4602'>Zovirax treatment</a>. <a href='http://elegantcode.com/?p=4578'>Cialis price, coupon</a>. <a href='http://elegantcode.com/?p=4189'>Flexeril price</a>.<br />
<b>Trackbacks from:</b> <a href='http://social-blend.com/?p=1007'>Ultram For Sale</a>. <a href='http://www.quarterlives.com/?p=915'>Ultram For Sale</a>. <a href='http://4realz.net/?p=1958'>Ultram For Sale</a>. <a href='http://tayloegray.com/?p=317'>Ultram For Sale</a>. <a href='http://www.macneilbmx.com/blog/?p=5108'>Ultram For Sale</a>. <a href='http://reversemortgagedaily.com/?p=14222'>Fast shipping Ultram</a>. <a href='http://linuxologist.com/?p=976'>Ultram pics</a>. <a href='http://home.officesnapshots.com/?p=138'>Ultram canada, mexico, india</a>. <a href='http://evanrapoport.com/?p=382'>Is Ultram addictive</a>. <a href='http://www.themusclecarplace.com/?p=4011'>Is Ultram addictive</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/26/sqlbulkcopy-for-generic-listt-useful-for-entity-framework-nhibernate/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Buy Viagra Without Prescription</title>
		<link>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor</link>
		<comments>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 22:51:00 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/</guid>
		<description><![CDATA[Buy Viagra Without Prescription, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with [...]]]></description>
			<content:encoded><![CDATA[<p> <p> <b>Buy Viagra Without Prescription</b>, If you’ve taken the jump to try out the new Entity Framework Code First and you’re allowing it to generate your database for you, you’ve most certainly run into the lack of migrations/updating existing schema support. Currently EF Code First will only create a database and won’t update a database with changes necessary to bring it in line with your model, <b>where can i order Viagra without prescription</b>.  <b>Effects of Viagra</b>, I know they’re working on it, but since it’s not there, <b>buy generic Viagra</b>, <b>Online buy Viagra without a prescription</b>, I thought I’d share a possible solution, albeit less polished than some of the well known database change management out there.</p>  <h4>Where is the tool?</h4>  <p>You can access it in the VS Command window, <b>Viagra coupon</b>.  <b>Buy Viagra no prescription</b>, On my x64 machine the tool is in.</p>  <blockquote>   <p>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Deploy\vsdbcmd.exe</p> </blockquote>  <h4>I want to deploy an existing schema to AppHarbor.</h4>  <p>Some high level steps that you can use for deployment of database changes.</p>  <ol>   <li>Generate an original reflection of your database. (*.dbschema file) </li>    <li>Tiny little hack to the .dbschema file, <b>Viagra reviews</b>. </li>    <li>Generate the change file to AppHarbor </li>    <li>Review Change Script Generated </li>    <li>Take the app offline, <b>Buy Viagra Without Prescription</b>.  <b>Order Viagra from mexican pharmacy</b>, (optional) </li>    <li>Apply Change Script </li>    <li>Bring the app online (mandatory if you took step 5) </li> </ol>  <h5>Generate an original reflection of your database.</h5>  <p>This file is a complete reflection of your databases schema in a single xml file.</p>  <p>The following command can be used to generate this file.</p>  <blockquote>   <p>vsdbcmd.exe      <br />&#160;&#160;&#160;&#160; /Action:Import       <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;Data Source=.\sqlexpress;Initial Catalog=MyDatabase;Integrated Security=True;Pooling=False&quot;       <br />&#160;&#160;&#160;&#160; /ModelFile:MyDatabase.dbschema</p> </blockquote>  <p>There are a ton of knobs to turn with this command line tool. Feel free to check out the docs <a href="http://msdn.microsoft.com/en-us/library/dd193283.aspx">http://msdn.microsoft.com/en-us/library/dd193283.aspx</a></p>  <p>Now you should have a file “MyDatabase.dbschema” sitting on your hard drive.</p>  <h5>Tiny little hack to the .dbschema file.</h5>  <p>The section of xml we want to manually remove from the file is related to where your mdf and ldf database files should exist on disk, <b>kjøpe Viagra på nett, köpa Viagra online</b>.  <b>Cheap Viagra</b>, When we go to deploy up to AppHarbor, if this is not removed, <b>buy Viagra online no prescription</b>, <b>Online buying Viagra hcl</b>, then vsdbcmd will generate script to attempt to move the files into the “correct” location. This operation will throw exceptions if you attempt to execute against AppHarbor as you don’t have permission to do this, <b>Viagra gel, ointment, cream, pill, spray, continuous-release, extended-release</b>.  <b>Buy Viagra Without Prescription</b>, We’re removing it from the xml file, as I can’t seem to get the correct command line option to ignore this (if there is an option).  <b>Viagra no prescription</b>, So by removing it, it’s just not used and completely ignored.</p>  <p>I don’t know if this will be true for everyone, <b>Viagra photos</b>, <b>Viagra steet value</b>, but I find that the last two sections of xml in the dbschema file are all I have to remove. I’ll show the two full sections below so you can use it as a reference of what to remove from the file.</p>  <pre class="brush: xml;">&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultDataPath)$(DatabaseName).mdf&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;2304&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;1024&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Relationship Name=&quot;Filegroup&quot;&gt;        &lt;Entry&gt;            &lt;References ExternalSource=&quot;BuiltIns&quot; Name=&quot;[PRIMARY]&quot; /&gt;        &lt;/Entry&gt;    &lt;/Relationship&gt;&lt;/Element&gt;&lt;Element Type=&quot;ISql90File&quot; Name=&quot;[MyDatabase_log]&quot;&gt;    &lt;Property Name=&quot;FileName&quot; Value=&quot;$(DefaultLogPath)$(DatabaseName)_log.LDF&quot; /&gt;    &lt;Property Name=&quot;Size&quot; Value=&quot;576&quot; /&gt;    &lt;Property Name=&quot;SizeUnit&quot; Value=&quot;3&quot; /&gt;    &lt;Property Name=&quot;MaxSize&quot; Value=&quot;2097152&quot; /&gt;    &lt;Property Name=&quot;IsUnlimited&quot; Value=&quot;False&quot; /&gt;    &lt;Property Name=&quot;FileGrowth&quot; Value=&quot;10&quot; /&gt;    &lt;Property Name=&quot;FileGrowthUnit&quot; Value=&quot;1&quot; /&gt;    &lt;Property Name=&quot;IsLogFile&quot; Value=&quot;True&quot; /&gt;&lt;/Element&gt;</pre></p>
<p><h5>Generate the change file to AppHarbor.</h5></p>
<p><p>Now that we have a .dbschema file containing the complete model of what we want deployed, <b>comprar en línea Viagra, comprar Viagra baratos</b>, <b>Viagra without prescription</b>, we can now use it to generate a schema change deployment script.</p></p>
<p><blockquote>  <p>vsdbcmd.exe<br />
    <br />&#160;&#160;&#160;&#160; /Action:Deploy</p>
<p>    <br />&#160;&#160;&#160;&#160; /DeployToDatabase:-</p>
<p>    <br />&#160;&#160;&#160;&#160; /Script:Test.sql</p>
<p>    <br />&#160;&#160;&#160;&#160; /ConnectionString:&quot;{YourAppHarborConnectionString}&quot;</p>
<p>    <br />&#160;&#160;&#160;&#160; /ModelFile:HackIt.dbschema</p>
<p>    <br />&#160;&#160;&#160;&#160; /Properties:TargetDatabase={YourAppHarborDatabaseName EX:db1235}</p></blockquote></p>
<p><p>I’ll explain a couple of the above command options.</p></p>
<p><h6>/DeployToDatabase:-</h6></p>
<p><p>This one is _<strong><u>key</u></strong>_. This tells vsdbcmd to only generate a change script, <b>Viagra price, coupon</b>, <b>Herbal Viagra</b>, and <strong>not to actually deploy the changes </strong>immediately. Until you feel comfortable with what sql the tool generates, <b>Viagra australia, uk, us, usa</b>, <b>Viagra blogs</b>, which is usually pretty darn good, you should not apply it immediately, <b>Viagra dose</b>. Allow the tool to generate the file for further inspection and you can execute it manually after, <b>Buy Viagra Without Prescription</b>.  <b>Viagra dosage</b>, <br /></p></p>
<p><h6>/Script:Test.sql</h6></p>
<p><p>This this is just the name of the file to dump the deployment changes.<br />
  <br /></p></p>
<p><h6>/ModelFile:HackIt.dbschema</h6></p>
<p><p>The path to the .dbschema we generated and modified above.</p></p>
<p><p>&#160;</p></p>
<p><h5>Review Change Script Generated.</h5></p>
<p><p>After you’ve generated a change script file, <b>order Viagra from United States pharmacy</b>, <b>Buy Viagra from canada</b>, take a look at the sql just to make sure you’re happy with what it generates.</p></p>
<p><h5>Take the app offline. (optional)</h5></p>
<p><p>This one depends on the schema changes, <b>Viagra class</b>.  <b>Where can i find Viagra online</b>, If the changes are serious enough, you can check in an App_Offline.htm file at the root of web project and do a “git push appharbor”, <b>Viagra maximum dosage</b>.  <b>Buy Viagra Without Prescription</b>, This way, while making schema changes you don’t have to worry about the errors popping up on users.  <b>Is Viagra safe</b>, Down side is your site becomes inoperable.</p></p>
<p><blockquote>  <p>If you’ve never heard of the App_Offline.htm – I’d recommend reading up on it. <a href="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p></blockquote></p>
<p><h5>Apply Change Script.</h5></p>
<p><p>You have several options to actually apply the scripted changes.</p></p>
<p><ol>  <li>Use vsdbcmd to deploy – Just turn the /DeployToDatabase:<strong>-</strong> to /DeployToDatabase:<strong>+</strong> and allow vsdbcmd to apply the script right there, <b>Viagra over the counter</b>.  <b>Fast shipping Viagra</b>, </li></p>
<p>  <li>Use SQL Management Studio.<br />
    <br /><strong>Make Sure you turn on SQLCMD Mode</strong><a href="http://elegantcode.com/wp-content/uploads/2011/04/image1.png"><br />
      <br /><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2011/04/image_thumb1.png" width="311" height="333" /></a>&#160; </li></ol></p>
<p><h5>Bring your site back online.</h5></p>
<p><p>Now you can go re-name the App_Offline.htm to something like App_Offline.htm.disabled and push those changes back up to AppHarbor.</p></p>
<p><h4>Other considerations.</h4></p>
<p><ul>  <li>Consider a simple migrations framework (EX: <a href="http://code.google.com/p/dbup/" target="_blank">DbUp</a>) to get some initial data or things initialized, <b>generic Viagra</b>, <b>Viagra australia, uk, us, usa</b>, but be-ware that you’ll have to think a little harder about the “rollback” steps with this approach.</p>
<p>    <br /></li></p>
<p>  <li>Next you could take a look at an inquiry I made on the public support discussion list at AppHarbor hoping to push AppHarbor to implement this App_Offline.htm support (right into their admin site)<a href="http://support.appharbor.com/discussions/problems/373-deployment-feature-idea"><br />
      <br />http://support.appharbor.com/discussions/problems/373-deployment-feature-idea</a></p>
<p>    <br /><strong>UPDATE:</strong> SPARE A VOTE OR TWO: <a title="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic" href="http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic">http://feedback.appharbor.com/forums/95687-general/suggestions/1686365-add-feature-to-push-an-app-offline-htm-at-the-clic</a> </li></ul></p>
<p><p>One great benefit of to this approach is the ability for vsdbcmd to manage changes to an existing schema, <b>rx free Viagra</b>.  <b>Cheap Viagra</b>, </p></p>
<p><p>Now if you want full support like refactorings such as table, column, <b>Viagra treatment</b>, <b>Ordering Viagra online</b>, etc renames. You will want to keep a full db project and use that to do a deployment.</p></p>
<p><p>Hope you find this useful, <b>buy no prescription Viagra online</b>.  <b>Taking Viagra</b>, Happy Deployment!</p>.</p>
<p></p>
<p><b>Similar posts:</b> <a href='http://elegantcode.com/?p=4335'>Buy Differin Without Prescription</a>. <a href='http://elegantcode.com/?p=4423'>Antabuse For Sale</a>. <a href='http://elegantcode.com/?p=4744'>Buy Flexeril Without Prescription</a>. <a href='http://elegantcode.com/?p=4587'>Buy Pristiq Without Prescription</a>. <a href='http://elegantcode.com/?p=4633'>Cephalexin For Sale</a>. <a href='http://elegantcode.com/?p=4384'>Order Ultram online overnight delivery no prescription</a>. <a href='http://elegantcode.com/?p=4683'>Inderal schedule</a>. <a href='http://elegantcode.com/?p=4822'>About Hydrochlorothiazide</a>. <a href='http://elegantcode.com/?p=4728'>Ventolin australia, uk, us, usa</a>. <a href='http://elegantcode.com/?p=4694'>Human Growth Hormone interactions</a>.<br />
<b>Trackbacks from:</b> <a href='http://www.greatgreengoods.com/?p=2891'>Buy Viagra Without Prescription</a>. <a href='http://www.thegriffonnews.com/?p=10272'>Buy Viagra Without Prescription</a>. <a href='http://www.themusclecarplace.com/?p=3445'>Buy Viagra Without Prescription</a>. <a href='http://linuxologist.com/?p=363'>Buy Viagra Without Prescription</a>. <a href='http://blog.farmland.org/?p=2709'>Buy Viagra Without Prescription</a>. <a href='http://www.quarterlives.com/?p=286'>Where can i cheapest Viagra online</a>. <a href='http://www.macneilbmx.com/blog/?p=6199'>Online Viagra without a prescription</a>. <a href='http://social-blend.com/?p=1253'>Japan, craiglist, ebay, overseas, paypal</a>. <a href='http://www.leaduganda.org/?p=824'>Purchase Viagra online no prescription</a>. <a href='http://4realz.net/?p=1851'>Viagra no rx</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/13/using-vsdbcmd-to-deploy-an-entity-framework-ef-codefirst-or-any-other-database-to-appharbor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework GetById&lt;T&gt;</title>
		<link>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-getbyidt</link>
		<comments>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 07:00:50 +0000</pubDate>
		<dc:creator>Mike Moser</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Expression]]></category>
		<category><![CDATA[GetById]]></category>
		<category><![CDATA[Lamda]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4104</guid>
		<description><![CDATA[I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated. After searching some forums and blogs, I came [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked the other day if I knew of a way to do GetById generically with Entity Framework. I thought that I had done something similiar in the past and the task would be easy, but it turned out to be a little bit more complicated.</p>  <p>After searching some forums and blogs, I came up with a result that I like a bit. First we will start with a simple test:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> When_Getting_Product_By_Id : Rollback_Specification_Context&lt;Container&gt;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Return_Product()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.IsNotNull(_product);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    [Test]</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Should_Have_Proper_Id()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Assert.AreEqual(_productId, _product.ProductId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> <span style="color: #0000ff">long</span> _productId;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">private</span> Product _product;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Because()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _product = Repository.GetById&lt;Product, <span style="color: #0000ff">long</span>&gt;(_productId);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Context()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">base</span>.Context();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        _productId = 1498;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I will create&#160; an IRepository, which will be the base interface for all of my entity repositories:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    T GetById&lt;T, TId&gt;(TId id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then an IProductRepository, which is empty for the moment, but would have other items in it in the future:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IProductRepository : IRepository</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I wanted to use Expressions to put together a dynamic lamda expression, so I have a method to get the property info. Some of this idea was stolen and changed from a forum post that I read. What it is ultimately doing is looking for the Primary Key column for the particular entity that is passed in and returning the info for that property:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>Then I create the method for getting the id generically. At first, I just had GetById&lt;T&gt;(long id), but then I didn’t want someone to be held back by the type of id, in case they wanted to use a Guid or a long or anything else they desired:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                   select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>I have tried to document what I am doing for those that are unfamiliar with Expressions. I am ultimately building up the lamda expression of “x =&gt; x.Id == id”, where the id is the value passed in and the x.Id is the property that has the key on it.</p>

<p>I then go through the BaseEntitySets on the container looking for the set that matches my entity. Careful on this part, your namespace for your container needs to match the project name that your models are in.</p>

<p>Once I find the set, I can then create the query and apply the lamda expression to find the product that I am looking for.</p>

<p>So the final code looks like this:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">partial</span> <span style="color: #0000ff">class</span> Container</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> T GetById&lt;T, TId&gt;(TId id)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var type = <span style="color: #0000ff">typeof</span> (T);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        ParameterExpression argument = Expression.Parameter(type, <span style="color: #006080">&quot;x&quot;</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Expression expression = argument;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Property(expression, GetKeyPropertyInfo(type));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var valueExpression = Expression.Constant(id);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        expression = Expression.Equal(expression, valueExpression);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        </pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #008000">// x =&gt; x.Id == id</span></pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var lamda = Expression.Lambda&lt;Func&lt;T, <span style="color: #0000ff">bool</span>&gt;&gt;(expression, argument);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var container = <span style="color: #0000ff">this</span>.MetadataWorkspace.GetEntityContainer(<span style="color: #0000ff">this</span>.DefaultContainerName, DataSpace.CSpace);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var set = (from meta <span style="color: #0000ff">in</span> container.BaseEntitySets</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       <span style="color: #0000ff">where</span> meta.ElementType.FullName == type.ToString()</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                       select meta).First();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.CreateQuery&lt;T&gt;(<span style="color: #006080">&quot;[&quot;</span> + set.Name + <span style="color: #006080">&quot;]&quot;</span>).FirstOrDefault(lamda);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&#160;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> PropertyInfo GetKeyPropertyInfo(Type type)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        var properties = type.GetProperties();</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">foreach</span> (var propertyInfo <span style="color: #0000ff">in</span> properties)</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            var attributes = propertyInfo.GetCustomAttributes(<span style="color: #0000ff">true</span>);</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            <span style="color: #0000ff">if</span> (attributes.Any(attribute =&gt; attribute <span style="color: #0000ff">is</span> EdmScalarPropertyAttribute &amp;&amp; ((EdmScalarPropertyAttribute)attribute).EntityKeyProperty))</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            {</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">                <span style="color: #0000ff">return</span> propertyInfo;</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">            }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(String.Format(<span style="color: #006080">&quot;No key property found for type {0}&quot;</span>, type.Name));</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>
<!--CRLF-->

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
<!--CRLF--></div>
</div>

<p>And now my ProductRepository is a Partial of the Container and the ProductRepository implements the IProductRepository, so it has the GetById on it. The tests pass with flying colors of GREEN, so I am done.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/01/17/entity-framework-getbyidt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Entity Framework and the &#8220;The invoked member is not supported in a dynamic assembly&#8221; exception.</title>
		<link>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception</link>
		<comments>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 01:37:57 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/</guid>
		<description><![CDATA[When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create [...]]]></description>
			<content:encoded><![CDATA[<p>When you create an entity model, a few mapping files are created for you; namely .cdsl, .ssdl, and .msl.&#160; Right about now you may be saying to yourself, I don’t see those files anywhere. That is because they are actually stored as a resource in the assembly you created your entity model.&#160; When you create your entity model, an entity connection string is also created for you and placed in an App.config file, and may resemble the following:</p>  <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://*/BAT.DataModel.csdl|res://*/BAT.DataModel.ssdl|res://*/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>

<p>The entity connection string tells the EF which assembly to look for the mapping files. By default it is represented by the asterisk (*). The bad thing about this is that the * instructs EF to look in all assemblies in your application domain until it finds them. So, you’re probably thinking that this can cause performance issues, and you are absolutely right about that. But the real problem is when your application uses dynamic assemblies and cannot give out their resources. In this case an exception will be thrown, “The invoked member is not supported in a dynamic assembly”.</p>

<p>So how do we fix this? Simple, just fully qualify your assembly name in your entity connection string by replacing the * with your assembly name as follows:</p>

<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
  <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;BATEntities&quot;</span> <span style="color: #ff0000">connectionString</span><span style="color: #0000ff">=&quot;metadata=res://MyAssembly/BAT.DataModel.csdl|res://MyAssembly/BAT.DataModel.ssdl|res://MyAssembly/BAT.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=CREATIV-DESKTOP\MSSQLSERVER2008;Initial Catalog=BoiseAutoTrader;Integrated Security=True;MultipleActiveResultSets=True&amp;quot;&quot;</span> <span style="color: #ff0000">providerName</span><span style="color: #0000ff">=&quot;System.Data.EntityClient&quot;</span> <span style="color: #0000ff">/&gt;</span></pre>

    <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">connectionStrings</span><span style="color: #0000ff">&gt;</span></pre>
  </div>
</div>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/01/28/the-entity-framework-and-the-the-invoked-member-is-not-supported-in-a-dynamic-assembly-exception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity Extension for Entity Framework POCO Configuration, Repository and Unit of Work</title>
		<link>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work</link>
		<comments>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:35:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/</guid>
		<description><![CDATA[In my previous two posts I talk about simple EF4 mappings and some common abstractions I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous two posts I talk about <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">simple EF4 mappings</a> and some <a href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/">common abstractions</a> I like to work with for my entity persistence operations. In this post I want to take all of the setup code necessary for EF configuration and registration and encapsulate it along with the standard boiler plate IOC container code you might see in an application BootStrapper.</p>  <p>&#160;</p>  <h4>A Typical DI Example</h4>  <p>Though its pretty typical, I thought it would be good to show an example of how I expect to work with a Repository in my application. The “End Result” if you will.</p>  <p>The following is how one of my MVC.NET controllers might look (same for domain services, WCF services etc). Dependencies are satisfied via constructor injection when the controller or service is built up by the infrastructure (ControllerFactory or perhaps WCF IInstanceProvider).</p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> RosterController</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   <span class="kwrd">private</span> <span class="kwrd">readonly</span> IRepository&lt;Team&gt; _teamRepository;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> RosterController(IRepository&lt;Team&gt; teamRepository)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _teamRepository = teamRepository;</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> AddNewPlayer(<span class="kwrd">long</span> teamId, <span class="kwrd">string</span> playerName, <span class="kwrd">string</span> position)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       var team = _teamRepository.First(x =&gt; x.ID == teamId);</pre>

    <pre class="alt">       team.Players.Add(<span class="kwrd">new</span> Player{ Name = playerName, Position = position });</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>In order to “wire up” the above example, there is quite a bit that has to happen. </p>

<p>&#160;</p>

<h4></h4>

<h4>The Unity Extension</h4>

<p>Unity provides extension points, called UnityContainerExtension’s which are a perfect place to handle IOC initialization for a given component. For those familiar with Castle Windsor facilities, its kind of like that (or StructureMap registries). </p>

<p>The EFRepositoryExtension (could probably use a better name?) handles:</p>

<ul>
  <li>Creating the ContextBuilder and registering it with the Container </li>

  <li>Registering the Generic Repository&lt;T&gt; and UnitOfWork </li>

  <li>Context Lifetime </li>

  <li>Entity Configuration &amp; EntitySet Registration </li>

  <li>Context BuildUp </li>
</ul>

<p>The end result is nice clean extension I can register in my BootStrapper like so:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alteven">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alt">         .WithConnection(cnxString)</pre>

    <pre class="alteven">         .WithContextLifetime(<span class="kwrd">new</span> HttpContextLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alt">         .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alteven">         .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>
  </div>
</div>

<div>A pretty nice API to work with if I get to say so ;)</div>

<div>&#160;</div>

<h5>Fluent Interface</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IEFRepositoryExtension : IUnityContainerExtensionConfigurator</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString);</pre>

    <pre class="alteven">    IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager);</pre>

    <pre class="alt">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config);</pre>

    <pre class="alteven">    IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, <span class="kwrd">string</span> setName);</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p></p>

<h5>Extension Implementation</h5>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> EFRepositoryExtension : UnityContainerExtension, IEFRepositoryExtension</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">  <span class="kwrd">private</span> ContextBuilder&lt;ObjectContext&gt; _builder;</pre>

    <pre class="alteven">  <span class="kwrd">private</span> SqlConnection _connection;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Initialize()</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      _builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">      <span class="rem">//register the builder instance as a singleton, this will hold all of our </span></pre>

    <pre class="alteven">      <span class="rem">//mapping information for the duration of our application as it creates </span></pre>

    <pre class="alt">      <span class="rem">//new data contexts</span></pre>

    <pre class="alteven">      Container.RegisterInstance(<span class="str">&quot;builder&quot;</span>, _builder, </pre>

    <pre class="alt">                                 <span class="kwrd">new</span> ContainerControlledLifetimeManager());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">      <span class="rem">//Register Repo &amp; UOW. Those these are transient instances, they both take</span></pre>

    <pre class="alteven">      <span class="rem">//a ctor dependency on the ObjectContext which has its lifetime controlled</span></pre>

    <pre class="alt">      <span class="rem">//by the Extension. E.g., for an Http current request, all repository and</span></pre>

    <pre class="alteven">      <span class="rem">//UOW will use the same context/transaction</span></pre>

    <pre class="alt">      Container.RegisterType(<span class="kwrd">typeof</span>(IRepository&lt;&gt;), <span class="kwrd">typeof</span>(Repository&lt;&gt;));</pre>

    <pre class="alteven">      Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension WithConnection(<span class="kwrd">string</span> connectionString)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      _connection = <span class="kwrd">new</span> SqlConnection(connectionString);</pre>

    <pre class="alteven">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alt">  }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//simple pluralization of the entity set</span></pre>

    <pre class="alteven">      ConfigureEntity(config, <span class="kwrd">typeof</span>(T).Name+<span class="str">&quot;s&quot;</span>);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension ConfigureEntity&lt;T&gt;(EntityConfiguration&lt;T&gt; config, </pre>

    <pre class="alt">                                                   <span class="kwrd">string</span> setName)</pre>

    <pre class="alteven">  {</pre>

    <pre class="alt">      <span class="rem">//add the configuration</span></pre>

    <pre class="alteven">      _builder.Configurations.Add(config);</pre>

    <pre class="alt">      <span class="rem">//register the set metadata</span></pre>

    <pre class="alteven">      _builder.RegisterSet&lt;T&gt;(setName);</pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="kwrd">public</span> IEFRepositoryExtension WithContextLifetime(LifetimeManager lifetimeManager)</pre>

    <pre class="alt">  {</pre>

    <pre class="alteven">      Container.AddNewExtension&lt;StaticFactoryExtension&gt;();</pre>

    <pre class="alt">      Container.Configure&lt;IStaticFactoryConfiguration&gt;()</pre>

    <pre class="alteven">               .RegisterFactory&lt;IObjectContext&gt;(x =&gt; </pre>

    <pre class="alt">                   ContextResolver(x, lifetimeManager, _connection));</pre>

    <pre class="alteven">      </pre>

    <pre class="alt">      <span class="kwrd">return</span> <span class="kwrd">this</span>;</pre>

    <pre class="alteven">  }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">  <span class="rem">//factory func to build context with given lifetime &amp; connection</span></pre>

    <pre class="alt">  <span class="kwrd">static</span> <span class="kwrd">readonly</span> Func&lt;IUnityContainer, LifetimeManager, SqlConnection, <span class="kwrd">object</span>&gt; </pre>

    <pre class="alteven">      ContextResolver = (c, l, s) =&gt;</pre>

    <pre class="alt">      {</pre>

    <pre class="alteven">          var context = l.GetValue();</pre>

    <pre class="alt">          <span class="kwrd">if</span> (context == <span class="kwrd">null</span>)</pre>

    <pre class="alteven">          {</pre>

    <pre class="alt">              var builder = c.Resolve&lt;ContextBuilder&lt;ObjectContext&gt;&gt;(<span class="str">&quot;builder&quot;</span>);</pre>

    <pre class="alteven">              var newContext = builder.Create(s);</pre>

    <pre class="alt">              context = <span class="kwrd">new</span> ObjectContextAdapter(newContext);</pre>

    <pre class="alteven">              l.SetValue(context);</pre>

    <pre class="alt">          }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">          <span class="kwrd">return</span> context;</pre>

    <pre class="alteven">      };</pre>

    <pre class="alt">}</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Test</h4>

<p>Doing simple service location &amp; using TestLifetime, my prototype (not really a “test” I suppose)looks like:</p>

<div>
  <div class="csharpcode">
    <pre class="alt">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alteven">var container = IoC.Container; <span class="rem">//some static, thread safe container class</span></pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">container.AddNewExtension&lt;EFRepositoryExtension&gt;();</pre>

    <pre class="alt">container.Configure&lt;IEFRepositoryExtension&gt;()</pre>

    <pre class="alteven">&#160;&#160;&#160; .WithConnection(cnxString)</pre>

    <pre class="alt">&#160;&#160;&#160; .WithContextLifetime(<span class="kwrd">new</span> TestLifetimeManager&lt;IObjectContext&gt;())</pre>

    <pre class="alteven">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> TeamConfiguration())</pre>

    <pre class="alt">&#160;&#160;&#160; .ConfigureEntity(<span class="kwrd">new</span> PlayerConfiguration());</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.RegisterType&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">var rosterController = container.Resolve&lt;RosterController&gt;();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">rosterController.AddNewPlayer(3, <span class="str">&quot;Austin Collie&quot;</span>, <span class="str">&quot;&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">container.Resolve&lt;IUnitOfWork&gt;().Commit();</pre>
  </div>
</div>

<p>&#160;</p>

<p>Thoughts and feedback are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 19:53:02 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/</guid>
		<description><![CDATA[In my previous post I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability. A popular pattern for ORM data [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/">previous post</a> I demonstrated how to create a simple mapping using the latest EF4 CTP. In this post I will look at how I can customize some infrastructure code in an attempt to align EF POCO “Code Only” with existing patterns, while potentially increasing reuse and testability.</p>  <p>A popular pattern for ORM data access is the Repository pattern. Implementing a generic repository in EF4 gets much easier with ObjectSet&lt;T&gt;. One might argue that it is, in a sense, a Repository. </p>  <p>What I would like to do is “Adapt” this interface to my own generic Repository. This will let me customize the interface to my own specifications and remove the dependency in my domain services on ObjectSet&lt;T&gt;. </p>  <p>Here is a <a href="http://devtalk.dk/CommentView,guid,b5d9cad2-e155-423b-b66f-7ec287c5cb06.aspx">great article</a> based on the last CTP that does a nice job of laying out a Repository&lt;T&gt; strategy with a UnitOfWork, and here is <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/0001/01/01/the-repository-pattern.aspx">another</a> using nHibernate (see also <a href="http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx">UnitOfWork</a> examples). If you are not familiar with the Generic Repository pattern and UnitOfWork, you should take a quick second and skim these before you continue. I want to take this implementation and try and change just a few things about it. </p>  <ul>   <li><strong>Static Unit of Work reference</strong>: First, I want to remove the Repository static reference to the UnitOfWork, which is how the Repository in the examples are resolving the ObjectContext/Session. Aside from not being a fan of static classes, I think it is the application Container’s responsibility to manage dependency resolution. Plus, I don't really know that I want my Repository to have <em>any </em>reference to the UnitOfWork. I want the UnitOfWork to span across multiple repositories within a given transaction. </li>    <li><strong>Object Lifetime</strong>: I would like to have more granular control over the UnitOfWork and ObjectContext lifetime. In the authors example, the UnitOfWork is managing the lifetime strategy, which is something the Container is perfect at handling. I will leave the UOW to transaction management only. </li>    <li><strong>Constructor Injection</strong>: I think since the Repository has a hard dependency on the ObjectContext, that it would make the most sense to have the ObjectContext injected into Repository ctor. </li>    <li><strong>Concrete ObjectContext reference</strong>: Since I will now be passing the ObjectContext into the ctor, I feel better if this was an interface. Not that I foresee a tremendous amount of testable value here, but I still don't like taking a ctor dependency on a framework object if I can avoid it. Plus, this is prototype code, so why not? </li> </ul>  <h4>Repository Interface</h4>  <p>You will notice the interface remains unchanged from the first referenced article <em>(Update: changed Func&lt;T, bool&gt; to Expression&lt;Func&lt;T, bool&gt;&gt; so that expressions can be evaluated correctly by ObjectQuery)</em></p>  <div>   <div class="csharpcode">     <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IQueryable&lt;T&gt; AsQueryable();</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    IEnumerable&lt;T&gt; GetAll();</pre>

    <pre class="alteven">    IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">    T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alteven">    T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Delete(T entity);</pre>

    <pre class="alt">    <span class="kwrd">void</span> Add(T entity);</pre>

    <pre class="alteven">    <span class="kwrd">void</span> Attach(T entity);</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Generic Repository Implementation</h4>

<p>As I described in the bullets above, my implementation does in fact take the IObjectContext as a ctor argument. Also, notice that I do not have any static references.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Repository&lt;T&gt; : IRepository&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">{</pre>

    <pre class="alt">   IObjectSet&lt;T&gt; _objectSet;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> Repository(IObjectContext objectContext)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet = objectContext.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IQueryable&lt;T&gt; AsQueryable()</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet;</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; GetAll()</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.ToList();</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> IEnumerable&lt;T&gt; Find(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.Where(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> T Single(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       <span class="kwrd">return</span> _objectSet.Single(<span class="kwrd">where</span>);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> T First(Expression&lt;Func&lt;T, <span class="kwrd">bool</span>&gt;&gt; <span class="kwrd">where</span>)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       <span class="kwrd">return</span> _objectSet.First(<span class="kwrd">where</span>);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Delete(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.DeleteObject(entity);</pre>

    <pre class="alteven">   }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">   <span class="kwrd">public</span> <span class="kwrd">void</span> Add(T entity)</pre>

    <pre class="alt">   {</pre>

    <pre class="alteven">       _objectSet.AddObject(entity);</pre>

    <pre class="alt">   }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">   <span class="kwrd">public</span> <span class="kwrd">void</span> Attach(T entity)</pre>

    <pre class="alteven">   {</pre>

    <pre class="alt">       _objectSet.Attach(entity);</pre>

    <pre class="alteven">   }</pre>
}</div>
</div>

<p>&#160;</p>

<h4>Unit of Work</h4>

<p>Just like EF4 ObjectSet does a lot out of box for our Repository, the EF ObjectContext really does a lot of the work for us in regards to managing the UnitOfWork. The Object context is already capable of handling a transaction across many operations over different types. So, again, we are going to just add a little wrapper around the context and call it a UnitOfWork. This will give me a nice hook where my infrastructure can snag a reference to the ObjectContext and Commit the transaction changes, for example, at the end of a WCF, Web or DataService request.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">interface</span> IUnitOfWork</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">void</span> Commit();</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> UnitOfWork: IUnitOfWork, IDisposable</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IObjectContext _objectContext;</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> UnitOfWork(IObjectContext objectContext)</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext = objectContext;</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">if</span> (_objectContext != <span class="kwrd">null</span>)</pre>

    <pre class="alteven">        {</pre>

    <pre class="alt">            _objectContext.Dispose();</pre>

    <pre class="alteven">        }</pre>

    <pre class="alt">        GC.SuppressFinalize(<span class="kwrd">this</span>);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Commit()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _objectContext.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>The Object Context Adapter</h4>

<p>Just some Adapter 101 code here to abstract away the Concrete Context</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">interface</span> IObjectContext : IDisposable</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span>;</pre>

    <pre class="alteven">    <span class="kwrd">void</span> SaveChanges();</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> ObjectContextAdapter : IObjectContext</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">readonly</span> ObjectContext _context;</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> ObjectContextAdapter(ObjectContext context)</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        _context = context;</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.Dispose();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">    <span class="kwrd">public</span> IObjectSet&lt;T&gt; CreateObjectSet&lt;T&gt;() <span class="kwrd">where</span> T : <span class="kwrd">class</span></pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        <span class="kwrd">return</span> _context.CreateObjectSet&lt;T&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">void</span> SaveChanges()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        _context.SaveChanges();</pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<h4>&#160;</h4>

<h4>Using the Repository</h4>

<p>Without out my application Container, here is how I would code up the new abstractions:</p>

<div>
  <div>
    <div class="csharpcode">
      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven"><span class="rem">// a bunch of ugly set up code here, see previous post</span></pre>

      <pre class="alt"><span class="rem">//...</span></pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var context = builder.Create(connection);</pre>

      <pre class="alteven">var contextAdapter = <span class="kwrd">new</span> ObjectContextAdapter(context);</pre>

      <pre class="alt">var unitOfWork = <span class="kwrd">new</span> UnitOfWork(contextAdapter);</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var teamRepository = <span class="kwrd">new</span> Repository&lt;Team&gt;(contextAdapter);</pre>

      <pre class="alteven">var newTeam = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Da Bears&quot;</span> };</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">teamRepository.Add(newTeam);</pre>

      <pre class="alt">unitOfWork.Commit();</pre>

      <pre class="alteven">&#160;</pre>

      <pre class="alt">var team = teamRepository.First(x =&gt; x.Name == <span class="str">&quot;Da Bears&quot;</span>);</pre>

      <pre class="alteven">Console.WriteLine(team.Name);</pre>

      <pre class="alt">&#160;</pre>

      <pre class="alteven">context.Dispose();</pre>
    </div>
  </div>
</div>

<p><strong></strong></p>

<p><strong>Admittedly, this is not very pretty and is still way more than I want to deal with on regular basis. </strong>In my next post I will demonstrate how I will tie all this together, <a href="http://elegantcode.com/2009/12/15/building-a-unity-extension-for-entity-framework-poco-configuration-repository-and-unit-of-work/">EF Configuration, Repository and UnitOfWork using a Unity Extension</a>. </p>

<p>Please keep in mind this is prototype, untested code on a CTP release. Code at your own risk!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Entity Framework POCO (EF4): A Simple Mapping</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco-ef4-a-simple-mapping</link>
		<comments>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 06:47:12 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/</guid>
		<description><![CDATA[The latest EF4 CTP released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. In this post I want to take a look a [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/adonet/archive/2009/11/12/updated-feature-ctp-walkthrough-code-only-for-entity-framework.aspx">latest EF4 CTP</a> released on November 12th includes updated support for POCO’s (Plain Ol’ CLR Objects). Although we could use POCO’s in the previous CTP, we still had to create an EDMX artifact to model our entities, thus leading to a bit of duplication. </p>  <p>In this post I want to take a look a simple mapping scenario, and how that looks using the new ‘Code Only’ API. By using the code only API I can have complete control over my entities, giving me the most flexibility in my mapping strategies, better testability and extensibility, and ultimately greater maintainability. No generated code. No XML files. Sweet ;)</p>  <p>Since I am a sports fanatic I am going to use one of my favorite models, the sports team. (Hey, I could be using Orders!)</p>  <h5></h5>  <h4>The Database Table</h4>  <p>I will start by defining a simple table with 2 columns, Name and ID:</p>  <div>   <div class="csharpcode">     <pre class="alt">CREATE TABLE [dbo].[Teams](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Name] [nvarchar](50) NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Team] PRIMARY KEY CLUSTERED ([Id] ASC)</pre>

    <pre class="alt">) ON [PRIMARY]</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>
  </div>
</div>

<p>&#160;</p>

<h4>The Entity</h4>

<p>When creating an Entity the default convention is that the Entity property names should match the database column names exactly. I will create a <strong>Team</strong> entity which does that like so:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Team</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> Id { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
</div>

<p></p>

<p></p>

<p></p>

<p></p>

<h4>The Mapping</h4>

<p>Mapping the Entity to the table is very easy. To do so, implement a Generic class EntityConfiguration&lt;T&gt; and setup the map in the ctor. For the <strong>Team</strong> entity, that will look like this:</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">{</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">public</span> TeamConfiguration()</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    {</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Id).IsIdentity();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    }</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">}</pre>
  </div>
The EntityConfiguration methods contain many overloads which provide a variety of mapping options. For now, I will leave it simple.</div>

<p>&#160;</p>

<h4>The Infrastructure</h4>

<p>Now that I have the entity and table mapped together, I need to provide the EF infrastructure with the configuration, and then I can begin working with the Entity(ies).</p>

<h5>Connection</h5>

<p>A simple connection string. I felt this was worth pointing out for those that have used EF3.5. Note that this is just a simple DB connection, and does not contain the metadata parts.</p>

<div>
  <div style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">
    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;configuration&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">    &lt;add name=<span style="color: #006080">&quot;Test&quot;</span> connectionString=<span style="color: #006080">&quot;Data Source=.;Initial Catalog=Test;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">        Integrated Security=True;MultipleActiveResultSets=True&quot;</span>/&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">  &lt;/connectionStrings&gt;</pre>

    <pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;/configuration&gt;</pre>
  </div>
</div>

<p>&#160;</p>

<h5>The Object Context</h5>

<p>If you look at the <a href="http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx">CTP walkthrough</a> you will notice the sample includes a class called BloggingModel which derives from ObjectContext. BloggingModel has properties for each ObjectSet&lt;T&gt;. I believe this is done so that the ContextBuilder can infer the EntitySets and build the correct metadata. To me this is an unnecessary class that I would rather not maintain. Instead, before I create the context,&#160; I will handle the metadata registration by calling “builder.RegisterSet&lt;T&gt;(&quot;SetName&quot;);” <em>(For now… see future post).</em>&#160; If I did not do this, the context would use the default naming convention for our EntitySet, and would expect a table named “TeamSet”.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="rem">//create builder</span></pre>

    <pre class="alteven">var builder = <span class="kwrd">new</span> ContextBuilder&lt;ObjectContext&gt;();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//add our Team set configuration</span></pre>

    <pre class="alt">builder.Configurations.Add(<span class="kwrd">new</span> TeamConfiguration());</pre>

    <pre class="alteven"><span class="rem">//keep in mind this call is to avoid custom object context</span></pre>

    <pre class="alt">builder.RegisterSet&lt;Team&gt;(<span class="str">&quot;Teams&quot;</span>);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//setup connection</span></pre>

    <pre class="alteven">var cnxString = ConfigurationManager.ConnectionStrings[<span class="str">&quot;Test&quot;</span>].ConnectionString;</pre>

    <pre class="alt">var connection = <span class="kwrd">new</span> SqlConnection(cnxString);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="rem">//create the context</span></pre>

    <pre class="alteven">var context = builder.Create(connection);</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//now we have a good context, and can go to work</span></pre>

    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">teams.AddObject(<span class="kwrd">new</span> Team { Name = <span class="str">&quot;New Orleans Saints&quot;</span> });</pre>

    <pre class="alt">context.SaveChanges();</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<p>Aside from some of the context setup code, it’s fairly straight forward. Simple data access with a POCO entity.&#160; check.</p>

<p>&#160;</p>

<h4>Extending the Entity with a One-to-Many Relationship</h4>

<p>Now that I have the basic mapping down, I want to go one step further and add a one-to-many relationship. What’s a team without players? </p>

<h5></h5>

<h5>Player Table</h5>

<div>
  <div class="csharpcode">
    <pre class="alt">CREATE TABLE [dbo].[Players](</pre>

    <pre class="alteven">    [ID] [bigint] IDENTITY(1,1) NOT NULL,</pre>

    <pre class="alt">    [Team_ID] [bigint] NOT NULL,</pre>

    <pre class="alteven">    [Name] [nvarchar](50) NOT NULL,</pre>

    <pre class="alt">    [Position] [nvarchar](50) NOT NULL,</pre>

    <pre class="alteven"> CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED ([ID] ASC)</pre>

    <pre class="alt">) </pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players]  WITH CHECK ADD  CONSTRAINT [FK_Players_Teams] FOREIGN KEY([Team_ID])</pre>

    <pre class="alteven">REFERENCES [dbo].[Teams] ([ID])</pre>

    <pre class="alt">GO</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">ALTER TABLE [dbo].[Players] CHECK CONSTRAINT [FK_Players_Teams]</pre>

    <pre class="alteven">GO</pre>
  </div>
</div>

<h5>Entities and Mappings</h5>

<p>I have added an ICollection to <strong>Team</strong> for the Players, and each <strong>Player</strong> has a 1-1 <strong>Team </strong>property. Mapping the collection is straight forward by using the “Relationship()”&#160; method which takes my expression for the properties I am mapping too.</p>

<div>
  <div class="csharpcode">
    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> Team</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Players = <span class="kwrd">new</span> Collection&lt;Player&gt;();</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> ICollection&lt;Player&gt; Players { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> Player</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">long</span> ID { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }</pre>

    <pre class="alteven">    <span class="kwrd">public</span> <span class="kwrd">string</span> Position { get; set; }</pre>

    <pre class="alt">    <span class="kwrd">public</span> Team Team { get; set; }</pre>

    <pre class="alteven">}</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="kwrd">public</span> <span class="kwrd">class</span> TeamConfiguration : EntityConfiguration&lt;Team&gt;</pre>

    <pre class="alt">{</pre>

    <pre class="alteven">    <span class="kwrd">public</span> TeamConfiguration()</pre>

    <pre class="alt">    {</pre>

    <pre class="alteven">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alt">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">        <span class="rem">// 1 to * relationships     </span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).IsOptional();</pre>

    <pre class="alteven">        <span class="rem">//set up inverse</span></pre>

    <pre class="alt">        Relationship(c =&gt; c.Players).FromProperty(x =&gt; x.Team);</pre>

    <pre class="alteven">    }</pre>

    <pre class="alt">}</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt"><span class="kwrd">public</span> <span class="kwrd">class</span> PlayerConfiguration : EntityConfiguration&lt;Player&gt;</pre>

    <pre class="alteven">{</pre>

    <pre class="alt">    <span class="kwrd">public</span> PlayerConfiguration()</pre>

    <pre class="alteven">    {</pre>

    <pre class="alt">        Property(c =&gt; c.ID).IsIdentity();</pre>

    <pre class="alteven">        Property(c =&gt; c.Name).HasMaxLength(50).IsRequired();</pre>

    <pre class="alt">        Property(c =&gt; c.Position).HasMaxLength(50).IsRequired();</pre>

    <pre class="alteven">               </pre>

    <pre class="alt">        Relationship(c =&gt; c.Team).IsRequired();</pre>

    <pre class="alteven">        Relationship(c =&gt; c.Team).FromProperty(x =&gt; x.Players); </pre>

    <pre class="alt">    }</pre>

    <pre class="alteven">}</pre>
  </div>
</div>

<div>&#160;</div>

<div>Now that the relationship is defined, I can work just as I would with a simple collection:</div>

<div>&#160;</div>

<div>
  <div class="csharpcode">
    <pre class="alt">var teams = context.CreateObjectSet&lt;Team&gt;();</pre>

    <pre class="alteven">var team = <span class="kwrd">new</span> Team { Name = <span class="str">&quot;Indianapolis Colts&quot;</span> };</pre>

    <pre class="alt">var player1 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Peyton Manning&quot;</span>, Position = <span class="str">&quot;QB&quot;</span>};</pre>

    <pre class="alteven">var player2 = <span class="kwrd">new</span> Player { Name = <span class="str">&quot;Reggie Wayne&quot;</span>, Position = <span class="str">&quot;WR&quot;</span> };</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">team.Players.Add(player1);</pre>

    <pre class="alt">team.Players.Add(player2);</pre>

    <pre class="alteven">&#160;</pre>

    <pre class="alt">teams.AddObject(team);</pre>

    <pre class="alteven">context.SaveChanges();</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven"><span class="rem">//create new player set to show we are pulling back from db</span></pre>

    <pre class="alt">var players = context.CreateObjectSet&lt;Player&gt;().Include(<span class="str">&quot;Team&quot;</span>)</pre>

    <pre class="alteven">                    .Where(x =&gt; x.Team.Name == <span class="str">&quot;Indianapolis Colts&quot;</span>)</pre>

    <pre class="alt">                    .ToList();</pre>

    <pre class="alteven">players.ForEach(x =&gt; Console.WriteLine(x .Name + <span class="str">&quot; - &quot;</span> + x.Team.Name));</pre>

    <pre class="alt">&#160;</pre>

    <pre class="alteven">context.Dispose();</pre>
  </div>
</div>

<div>&#160;</div>

<p>And that’s it. </p>

<p>While I was working on this sample I bumped into quite a few common scenarios which are not yet supported. Seeing how this is an early CTP, I suppose that is to be expected. That said, I am excited to see the direction it is taking and look forward to learning more as I have time to work with it.</p>

<p>For more information take a look at the <a href="http://blogs.msdn.com/adonet/archive/tags/Entity+Framework+Feature+CTP2+for+.NET+4.0/default.aspx">related EF posts</a> on the ADO.NET team blog.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2009/12/15/entity-framework-poco-ef4-a-simple-mapping/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Intro to The Entity Framework Presentation</title>
		<link>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=intro-to-the-entity-framework-presentation</link>
		<comments>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 06:09:20 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/</guid>
		<description><![CDATA[I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to present at the MSDN Unleashed event here in Boise. Thanks to everyone who made it down, it was a great day packed with new technology. (unfortunately my stay was cut short) </p>  <p>I was asked if I could make my slide deck and code samples available, so here they are. Enjoy. </p>  <p>Please let me know if you have any questions, or would like more info about a specific topic. (jarod at elegantcode)</p>  <p><a href="http://elegantcode.com/files/Jarod/IntroToEntityFramework.zip">Slide Deck</a></p>  <p><a href="http://elegantcode.com/files/Jarod/MSDNEF.zip">Code</a></p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/12/18/intro-to-the-entity-framework-presentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Entity Framework &#8211; POCO</title>
		<link>http://elegantcode.com/2008/08/05/entity-framework-poco/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-poco</link>
		<comments>http://elegantcode.com/2008/08/05/entity-framework-poco/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 07:12:59 +0000</pubDate>
		<dc:creator>Jarod Ferguson</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/08/05/entity-framework-poco/</guid>
		<description><![CDATA[The Entity Framework team is having an open design discussion regarding the best way to implement state management with POCO. A lot of community members have been very vocal about the initial release of EF, with lack of POCO support being a major pain point. (as well as Microsoft's inability to listen to community) Don't [...]]]></description>
			<content:encoded><![CDATA[<p>The Entity Framework team is having <a href="http://blogs.msdn.com/efdesign/archive/2008/08/01/discussion-about-api-changes-necessary-for-poco.aspx">an open design discussion</a> regarding the best way to implement state management with POCO. A lot of community members have been <a href="http://efvote.wufoo.com/forms/ado-net-entity-framework-vote-of-no-confidence/">very vocal</a> about the initial release of EF, with lack of POCO support being a major pain point. (as well as Microsoft's inability to listen to community)</p> <p>Don't be quiet now, this is an excellent opportunity to use your expertise to influence the next release. I was actually quite shocked to see the lack of feedback. Speak up people!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/08/05/entity-framework-poco/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

