<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Benny Michielsen</title>
	
	<link>http://blog.bennymichielsen.be</link>
	<description>.net developer with a touch of Cocoa</description>
	<lastBuildDate>Thu, 26 Jan 2012 18:55:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/BennyMichielsen" /><feedburner:info uri="bennymichielsen" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Getting started with node.js tutorials and books</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/HI-z4Q72nDA/</link>
		<comments>http://blog.bennymichielsen.be/2012/01/26/getting-started-with-node-js-tutorials-and-books/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 18:55:07 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/2012/01/26/getting-started-with-node-js-tutorials-and-books/</guid>
		<description><![CDATA[I found these great resources to get me going with node. Totally new to it? Take a look at this video from the creator of node himself. If you are unafraid and want a bit more lenghtly tutorial, in which &#8230; <a href="http://blog.bennymichielsen.be/2012/01/26/getting-started-with-node-js-tutorials-and-books/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I found these great resources to get me going with node.</p>
<p>Totally new to it? Take a look at <a href="http://www.youtube.com/watch?v=jo_B4LTHi3I&#038;feature=youtube_gdata_player">this video</a> from the creator of node himself.</p>
<p>If you are unafraid and want a bit more lenghtly tutorial, in which you actually create something following best practices, you should check out <a href="http://www.nodebeginner.org/">nodebeginner.org</a>. I was able to compare a lot of the instructions with my own way of working in .net.</p>
<p><a href="http://nodetuts.com/handson-nodejs-book.html">Hands on node.js</a> is an ebook with accompanying exercises, great for a more traditional way of learning. The first part of the book is free and the full version will only set you back a few dollars.</p>
<p>Finally there&#8217;s another Manning book under way, you can already grab the <a href="http://www.manning.com/cantelon/">MEAP</a>.</p>
<p>There&#8217;s plenty more out there but I found these to be the most helpful for me.</p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/HI-z4Q72nDA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2012/01/26/getting-started-with-node-js-tutorials-and-books/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2012/01/26/getting-started-with-node-js-tutorials-and-books/</feedburner:origLink></item>
		<item>
		<title>Legacy Code Retreat Leuven</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/OkCR1QISE6I/</link>
		<comments>http://blog.bennymichielsen.be/2011/11/28/legacy-code-retreat-leuven/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 20:46:46 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Legacy]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/2011/11/28/legacy-code-retreat-leuven/</guid>
		<description><![CDATA[On Saturday I spent the day near Leuven on the first publicly published Legacy code retreat. We were given an existing code base which seemed rather small, I guess around 400 lines of code, but after digging a bit deeper &#8230; <a href="http://blog.bennymichielsen.be/2011/11/28/legacy-code-retreat-leuven/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>On Saturday I spent the day near Leuven on the first publicly published <a href="http://www.legacycoderetreat.com/">Legacy code retreat</a>. We were given an existing code base which seemed rather small, I guess around 400 lines of code, but after digging a bit deeper my colleagues and I found out that it was quite a monster. </p>
<p>Several techniques were covered. Subclassing to override behaviour, introducing a golden master, moving behaviour in collaborating classes and making &#8220;pure&#8221; functions.</p>
<p>The golden master is probably best suited if you encounter a black box and need to make a change. The code base had a bunch of console.writelines so if you redirect the output and run the applications a number of times with different kinds of input, 10000 times was suggested as a good number, you end up with a number of test files. With those in place can then make the change and compare the new output with your golden master. Automate this and you have a reasonable case. It all depends on having some kind of instrumentation in place so you can harvest this kind of information.</p>
<p>Creating subclasses to override, or rather get around, methods in order to test which paths the program flow follows is something I had done before. Heck you even do that with &#8220;new&#8221; code when you&#8217;re stubbing/mocking, but we were not allowed to use any sort of framework so we had to hand roll them. This eventually leads to a situation where you&#8217;re testing more the subclass rather then the system under test.</p>
<p>The answer to that resulting code base is to abstract the code you have to other new classes which are injected through the constructor. Typical dependency injection and inversion.</p>
<p>The last technique was the most eye opening to me. It was suggested to make &#8220;pure functions&#8221;, meaning any method you wrote was not allowed to directly change the object state. Somehow I was able to see code duplication and underlying algorithms a lot faster throughout the code base. Probably need to look a bit into functional programming as it was quite interesting. </p>
<p>The last two iterations we were allowed to keep the code we were working on, in sharp contrast to a normal code retreat, wich gave us some more sense of accomplishment by improving the code base.</p>
<p>I really liked my first code retreat, gave me a change to work with people who work in different parts of the industry, embedded programming to name one and from different parts of Europe. </p>
<p>Looking forward to the next one.</p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/OkCR1QISE6I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/11/28/legacy-code-retreat-leuven/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/11/28/legacy-code-retreat-leuven/</feedburner:origLink></item>
		<item>
		<title>Plugging in your own Membership provider</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/78ZYvn5i44I/</link>
		<comments>http://blog.bennymichielsen.be/2011/06/02/plugging-in-your-own-membership-provider/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 10:11:34 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[membership]]></category>
		<category><![CDATA[provider]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=1009</guid>
		<description><![CDATA[Somebody who followed an ASP.NET course with me couldn&#8217;t find the necessary plugs to add your own providers to the .NET infrastructure. Since more will probably benefit I&#8217;m sharing with you all the necessary steps. So you have your users &#8230; <a href="http://blog.bennymichielsen.be/2011/06/02/plugging-in-your-own-membership-provider/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Somebody who followed an ASP.NET course with me couldn&#8217;t find the necessary plugs to add your own providers to the .NET infrastructure. Since more will probably benefit I&#8217;m sharing with you all the necessary steps.</p>
<p>So you have your users stored somewhere in a database and can&#8217;t use the out of the box implementations that ship with the .NET framework. What are the steps that you need to do?</p>
<p>First, create a class that inherits from the abstract <a href="http://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.aspx">MembershipProvider </a>and implement all the functionality you want to support. The more you add the better all the components that rely on it will be able to function. To just get beyond an MVC3 login page or a login with an ASP.NET webcontrol you just need to implement the validate user method.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//other methods omitted for brevity</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ValidateUser<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> username, <span style="color: #6666cc; font-weight: bold;">string</span> password<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//here you'll connect with your custom database or service</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var context <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyUserContext<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var user <span style="color: #008000;">=</span> context<span style="color: #008000;">.</span><span style="color: #0000FF;">MyUsers</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Where</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>username<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SingleOrDefault</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> user <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>For this demonstration I&#8217;m just checking if I have a user with the same username in my database.</p>
<p>With our class in place we now need to plug it into the provider model, open your web.config navigate to the system.web node and configure your provider. If you added any additional configuration properties in your custom provider you can configure them there as well.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;membership</span> <span style="color: #000066;">defaultProvider</span>=<span style="color: #ff0000;">&quot;MyMembershipProvider&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;providers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;clear</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyMembershipProvider&quot;</span> </span>
<span style="color: #009900;">             <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;MvcApplication2.MyMembershipProvider&quot;</span> </span>
<span style="color: #009900;">             <span style="color: #000066;">applicationName</span>=<span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/providers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/membership<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Done.</p>
<p>More guidance can be found on <a href="http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx">MSDN</a>.</p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/78ZYvn5i44I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/06/02/plugging-in-your-own-membership-provider/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/06/02/plugging-in-your-own-membership-provider/</feedburner:origLink></item>
		<item>
		<title>Entity Framework 4.1 – One To One Mapping</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/DNEAS4PBOOA/</link>
		<comments>http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 09:46:36 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[entity framework]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=1002</guid>
		<description><![CDATA[In the snippet below you can see that a customer has an address property. public class Customer &#123; public Customer&#40;&#41; &#123; Address = new Address&#40;&#41;; &#125; &#160; public Guid Id &#123; get; set; &#125; public string Name &#123; get; set; &#8230; <a href="http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the snippet below you can see that a customer has an address property.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
           Address <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Address<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Address Address <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid AddressId <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> City <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Country <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Street <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The database that gets generated though defines this as a one to many relationship.<br />
<a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/customeraddressonetomany.png" rel="lightbox[1002]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/customeraddressonetomany.png" alt="" title="customeraddressonetomany" width="220" height="356" class="aligncenter size-full wp-image-1003" /></a></p>
<p>Which is not what you&#8217;re expecting, or is it? I haven&#8217;t defined any restrictions on the address class so indeed an address could be shared between multiple customers. Let&#8217;s change that.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
           Address <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Address<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Address Address <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> City <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Country <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Street <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Customer Customer <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;ve added a navigation property from Address to Customer and marked the Customer property as required. This basically is telling EF that there is a one to one mapping between the two and the primary key of the customer should be used in the relationship. The primary key of the customer will also become the primary key of the address. If you did not add the required attribute you&#8217;ll get an invalidoperationexception saying that it&#8217;s unable to determine the principal end of an association.</p>
<p>You can also use the fluent api.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span>  <span style="color: #6666cc; font-weight: bold;">class</span>  CustomerContext
        <span style="color: #008000;">:</span> DbContext
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> IDbSet<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> Customers <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Address<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">HasRequired</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Customer</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>They both result in the same database structure.<br />
<a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/customersaddressonetoone.png" rel="lightbox[1002]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/customersaddressonetoone.png" alt="" title="customersaddressonetoone" width="225" height="340" class="aligncenter size-full wp-image-1004" /></a></p>
<p>What if both ends are required? A customer always has an address and an address always has a customer. Marking both ends as required will again result in the same exception as before.</p>
<p>In order to model this you have to use the fluent api and there are two ways to achieve the desired result.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span>  <span style="color: #6666cc; font-weight: bold;">class</span>  CustomerContext
        <span style="color: #008000;">:</span> DbContext
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> IDbSet<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> Customers <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Address<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">HasRequired</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Customer</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">WithRequiredDependent</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Address</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;m saying here that the address entity has a required customer property and that the customer class is actually in charge, <a href="http://msdn.microsoft.com/en-us/library/gg671273(v=VS.103).aspx">MSDN </a>reference.</p>
<p>Resulting database structure:<br />
<a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/onetoonerequired.png" rel="lightbox[1002]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/onetoonerequired.png" alt="" title="onetoonerequired" width="222" height="342" class="aligncenter size-full wp-image-1005" /></a></p>
<p>Another way to achieve the same result:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span>  <span style="color: #6666cc; font-weight: bold;">class</span>  CustomerContext
        <span style="color: #008000;">:</span> DbContext
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> IDbSet<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> Customers <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Address<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">HasRequired</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Customer</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">WithRequiredDependent</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">HasRequired</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Address</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">WithRequiredPrincipal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p><a href="http://msdn.microsoft.com/en-us/library/gg671256(v=VS.103).aspx">MSDN link</a> on WithRequiredPrincipal.</p>
<p>You saw that the WithRequiredPrincipal and Depedant actually have one taking a lambda and one with no arguments. This allows you to exclude a navigation property and still get a proper one to one mapping. Which brings me back to my starting point.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
           Address <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Address<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Address Address <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> City <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Country <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Street <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span>  <span style="color: #6666cc; font-weight: bold;">class</span>  CustomerContext
        <span style="color: #008000;">:</span> DbContext
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> IDbSet<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> Customers <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">HasRequired</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Address</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">.</span><span style="color: #0000FF;">WithRequiredPrincipal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p><a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/onetoonenonavigation.png" rel="lightbox[1002]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/onetoonenonavigation.png" alt="" title="onetoonenonavigation" width="221" height="336" class="aligncenter size-full wp-image-1006" /></a></p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/DNEAS4PBOOA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/</feedburner:origLink></item>
		<item>
		<title>Entity Framework 4.1 – Component mapping</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/v0-H--3QHOU/</link>
		<comments>http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-%e2%80%93-component-mapping/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 08:32:29 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[entity framework]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=996</guid>
		<description><![CDATA[By default if you add &#8216;simple&#8217; property types to your class (strings, ints,&#8230;), they are all translated into columns in the database. What if you had a group of properties that actually can have some functionality together. You also have &#8230; <a href="http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-%e2%80%93-component-mapping/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>By default if you add &#8216;simple&#8217; property types to your class (strings, ints,&#8230;), they are all translated into columns in the database. What if you had a group of properties that actually can have some functionality together. You also have this group of properties coming back at several places in your code base. You could introduce a base class in order to improve duplication but if the two entities are not really related you should stay away from overusing the inheritance approach. What you need here is a complextype. It was present in previous releases of the Entity Framework and can be used in 4.1. </p>
<p>We&#8217;ll modify the customer class posted below into something a bit more object oriented.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> City <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Country <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Street <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Customer could also have a bunch of methods that acted upon the address fields, Employee could have the same properties, the same logic and an invoice probably also has an address. So let&#8217;s move the properties, and logic, into a new class called Address. I don&#8217;t want a separate table that contains all the addresses though, I want all of the address fields to be present in the customer, employee and invoice table.</p>
<p>In order to achieve that result you need the ComplexType attribute.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Customer
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Customer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Address <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Address<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> Address Address <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#91;</span>ComplexType<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Address
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> City <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Country <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Street <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Using the ComplexType attribute will result in a database table with the correct structure.<br />
<a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/customerscomponent.png" rel="lightbox[996]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/06/customerscomponent.png" alt="" title="customerscomponent" width="210" height="152" class="aligncenter size-full wp-image-998" /></a></p>
<p>You can also use the fluent API to configure a complextype.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span>  <span style="color: #6666cc; font-weight: bold;">class</span>  CustomerContext
        <span style="color: #008000;">:</span> DbContext
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> IDbSet<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> Customers <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">ComplexType</span><span style="color: #008000;">&lt;</span>Address<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/v0-H--3QHOU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-%e2%80%93-component-mapping/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-%e2%80%93-component-mapping/</feedburner:origLink></item>
		<item>
		<title>Entity Framework 4.1 – Change mappings at runtime</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/YpIT3qpGWW4/</link>
		<comments>http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-change-mappings-at-runtime/#comments</comments>
		<pubDate>Sat, 21 May 2011 09:53:57 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=975</guid>
		<description><![CDATA[In v1 and v4 of EF I&#8217;ve been using this little gem, with a small tweak, to change the mappings of the model at runtime. I was wondering how I could achieve something similar in version 4.1 code first. By &#8230; <a href="http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-change-mappings-at-runtime/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In v1 and v4 of EF I&#8217;ve been using <a href="http://efmodeladapter.codeplex.com/" target="_blank">this little gem</a>, with a small tweak, to change the mappings of the model at runtime.</p>
<p>I was wondering how I could achieve something similar in version 4.1 code first. By design the OnModelCreating method is used to change the mappings of your model but it is only fired once per AppDomain since building it up does incur some performance penalty and normally your database schema does not change magically. In an application I&#8217;ve build though every customer gets his own tables, schemas etc. so hooking up some configuration logic in the OnModelCreating was out of the question since it&#8217;s only executed once.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//not possible, only executed once</span>
    var tableName <span style="color: #008000;">=</span> _schemaService<span style="color: #008000;">.</span><span style="color: #0000FF;">LookupTheTableName</span><span style="color: #008000;">&#40;</span>loggedInCustomer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span>tableName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Useless approach since the model isn&#8217;t being build again.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">MvcApplication1Context myContext <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MvcApplication1Context<span style="color: #008000;">&#40;</span>customer1, schemaService<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var csts <span style="color: #008000;">=</span> context<span style="color: #008000;">.</span><span style="color: #0000FF;">Customers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//will go to the same tables as before, because OnModelCreating is only executed once</span>
myContext <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MvcApplication1Context<span style="color: #008000;">&#40;</span>customer2, schemaService<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var otherCsts <span style="color: #008000;">=</span> context<span style="color: #008000;">.</span><span style="color: #0000FF;">Customers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>There is a constructor though on the DbContext class that takes a compiled model and using that one does allow you to tweak the mappings you are using in your application. This also allows you to work with models that have been generated with a designer from 4.0 to use with the DbContext API.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">//build config from scratch</span>
DbModelBuilder builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DbModelBuilder<span style="color: #008000;">&#40;</span>DbModelBuilderVersion<span style="color: #008000;">.</span><span style="color: #0000FF;">V4_1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Configurations</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> EntityTypeConfiguration<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//lets change something for each customer</span>
var schemaName <span style="color: #008000;">=</span> schemaService<span style="color: #008000;">.</span><span style="color: #0000FF;">LookupTheTableName</span><span style="color: #008000;">&#40;</span>loggedInCustomer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span>schemaName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">//use the constructor that takes a compiled model</span>
context <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MvcApplication1Context<span style="color: #008000;">&#40;</span>builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Build</span><span style="color: #008000;">&#40;</span>context<span style="color: #008000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Connection</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Compile</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You could then cache the compiled model for each customer so you don&#8217;t need to take that hit every time. Works like a charm. I&#8217;ll try to make it a bit easier to use. </p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/YpIT3qpGWW4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-change-mappings-at-runtime/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-change-mappings-at-runtime/</feedburner:origLink></item>
		<item>
		<title>Entity Framework 4.1 – Supporting Enums</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/A0IoWNHJi5M/</link>
		<comments>http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-supporting-enums/#comments</comments>
		<pubDate>Sat, 21 May 2011 08:20:02 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=970</guid>
		<description><![CDATA[One of those features you think are so trivial are hard to miss if you use that latest new tool. One of those is enum support in Entity Framework 4.1, just like in all other versions of the framework it &#8230; <a href="http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-supporting-enums/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>One of those features you think are so trivial are hard to miss if you use that latest new tool. One of those is enum support in Entity Framework 4.1, just like in all other versions of the framework it just isn&#8217;t there.</p>
<p>There is though a way to get around it, unfortunately it does mean changing your model just a tiny bit.</p>
<p>In the sample class model I used for my previous posts on mapping inheritance each movie had an enum value to hold its media type, DVD, Bluray,&#8230;</p>
<p>You won&#8217;t see it mapped by default in your database if you use Entity Framework 4.1 out of the box. So having the enum property there isn&#8217;t enough, you have to explicitly add an int property like below.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> EMediaType MediaType <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">int</span> MediaTypeId
<span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>MediaType<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> MediaType <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>EMediaType<span style="color: #008000;">&#41;</span>value<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And change your model mapping a bit inside your DbContext class if needed.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Movie<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Property</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">MediaTypeId</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">HasColumnName</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;MediaType&quot;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">IsRequired</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You can still use the enum property in the rest of your application, which I encourage.</p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/A0IoWNHJi5M" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-supporting-enums/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/05/21/entity-framework-4-1-supporting-enums/</feedburner:origLink></item>
		<item>
		<title>NuGet – install all packages referenced by a project</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/O3fEdVCoCDI/</link>
		<comments>http://blog.bennymichielsen.be/2011/05/20/nuget-install-all-packages-referenced-by-a-project/#comments</comments>
		<pubDate>Fri, 20 May 2011 18:43:28 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=966</guid>
		<description><![CDATA[When you install packages in a project using NuGet you probably don&#8217;t want to include those in your version control system. When you want to share some code in a blogpost you don&#8217;t want to include all the dlls that &#8230; <a href="http://blog.bennymichielsen.be/2011/05/20/nuget-install-all-packages-referenced-by-a-project/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When you install packages in a project using NuGet you probably don&#8217;t want to include those in your version control system. When you want to share some code in a blogpost you don&#8217;t want to include all the dlls that are referenced by your project since people can install the packages through NuGet.</p>
<p>When you install a package though a file gets added called packages.config containing all information needed by NuGet to find it in the repositories. There&#8217;s no out of the box solution though all the hooks are in place. You need to install the package called &#8220;NuGet.CommandLine&#8221;.</p>
<p><a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/nugetcommandline.png" rel="lightbox[966]"><img class="aligncenter size-full wp-image-967" title="nugetcommandline" src="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/nugetcommandline.png" alt="" width="517" height="39" /></a></p>
<p>This allows you to issue the nuget command which lets you refer to the packages.config file. Then use your package console manager to issue the following command: &#8220;nuget i PathToConfig -o Packages&#8221;.</p>
<p><a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/nugetpackagesinstall.png" rel="lightbox[966]"><img class="aligncenter size-full wp-image-968" title="nugetpackagesinstall" src="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/nugetpackagesinstall.png" alt="" width="411" height="215" /></a></p>
<p>You&#8217;ll see all your packages fly by. You can find more info over <a title="NuGet Documentation link" href="http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/O3fEdVCoCDI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/05/20/nuget-install-all-packages-referenced-by-a-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/05/20/nuget-install-all-packages-referenced-by-a-project/</feedburner:origLink></item>
		<item>
		<title>Entity Framework 4.1 Inheritance – Table per Concrete Type</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/yZgYsmNNj3w/</link>
		<comments>http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-concrete-type/#comments</comments>
		<pubDate>Fri, 20 May 2011 18:33:03 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=962</guid>
		<description><![CDATA[There&#8217;s one more way to implement the sample domain model in the database and that&#8217;s a table for each class with all properties. And unfortunatly our luck ends a bit here. Although it&#8217;s possible to generate an ERD that looks &#8230; <a href="http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-concrete-type/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s one more way to implement the sample domain model in the database and that&#8217;s a table for each class with all properties. And unfortunatly our luck ends a bit here. Although it&#8217;s possible to generate an ERD that looks like what we want to achieve I need to change the domain model.</p>
<p><a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/eftprt.png" rel="lightbox[962]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/eftprt.png" alt="" title="eftprt" width="535" height="676" class="aligncenter size-full wp-image-964" /></a></p>
<p>You get the database model above by using the following mapping code</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Movie<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span>  x<span style="color: #008000;">.</span><span style="color: #0000FF;">MapInheritedProperties</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Movies&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Cd<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">MapInheritedProperties</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Cds&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Book<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Map</span><span style="color: #008000;">&#40;</span>x<span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">MapInheritedProperties</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Books&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Pollymorphic associations though are not supported using this inheritance strategy and so you need to omit the collection of products in the category class, bummer.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Category
<span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> Guid Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #008080; font-style: italic;">//public virtual IList&lt;Product&gt; Products { get; set; }</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/yZgYsmNNj3w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-concrete-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-concrete-type/</feedburner:origLink></item>
		<item>
		<title>Entity Framework 4.1 Inheritance – Table per Type</title>
		<link>http://feedproxy.google.com/~r/BennyMichielsen/~3/VQ-05ICYwuY/</link>
		<comments>http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-type/#comments</comments>
		<pubDate>Fri, 20 May 2011 17:26:00 +0000</pubDate>
		<dc:creator>BennyM</dc:creator>
				<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.bennymichielsen.be/?p=957</guid>
		<description><![CDATA[Using the same model as before, we want to store it more in a normalized fashion and in my opinion something that closely resembles a typical inheritance structure in code. Lets create one table for each entity with only the &#8230; <a href="http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-type/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Using the same model as before, we want to store it more in a normalized fashion and in my opinion something that closely resembles a typical inheritance structure in code.</p>
<p><a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/model.png" rel="lightbox[957]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/model.png" alt="" title="model" width="757" height="198" class="aligncenter size-full wp-image-941" /></a><br />
<a href="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/dbtct.png" rel="lightbox[957]"><img src="http://blog.bennymichielsen.be/wp-content/uploads/2011/05/dbtct.png" alt="" title="dbtct" width="765" height="392" class="aligncenter size-full wp-image-958" /></a><br />
Lets create one table for each entity with only the properties it contains, all other ones are linked through a one to one mapping. To achieve this result you&#8217;ll need to create your mappings as illustrated below.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnModelCreating<span style="color: #008000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Movie<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Movies&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Cd<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Cds&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    modelBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;">&lt;</span>Book<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Books&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;">&#40;</span>modelBuilder<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<img src="http://feeds.feedburner.com/~r/BennyMichielsen/~4/VQ-05ICYwuY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.bennymichielsen.be/2011/05/20/entity-framework-4-1-inheritance-%e2%80%93-table-per-type/</feedburner:origLink></item>
	</channel>
</rss>

