<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Enterprise .Net</title><subtitle type="html">Morteza Manavi</subtitle><id>http://weblogs.asp.net/manavi/atom.aspx</id><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/default.aspx" /><link rel="self" type="application/atom+xml" href="http://weblogs.asp.net/manavi/atom.aspx" /><generator uri="http://communityserver.org" version="3.0.20510.895">Community Server</generator><updated>2010-12-08T03:25:00Z</updated><entry><title>NHibernate for Entity Framework Developers: Part 1 – Fluent NHibernate</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/10/02/nhibernate-for-entity-framework-developers-part-1-fluent-nhibernate.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/10/02/nhibernate-for-entity-framework-developers-part-1-fluent-nhibernate.aspx</id><published>2011-10-02T01:36:00Z</published><updated>2011-10-02T01:36:00Z</updated><content type="html"> 
&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
        &lt;tbody&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    The approach to managing persistent data has been a key design decision in every
                    software project we’ve worked on. Given that persistent data isn’t a new or unusual
                    requirement for .NET applications, you’d expect to be able to make a simple choice
                    among similar, well-established persistence solutions. Each of the competing solutions
                    has various advantages and disadvantages, but they all share the same scope and
                    overall approach.
                    &lt;br&gt;
                    &lt;br&gt;
                    Having said that, making the right decision requires a fairly good knowledge of
                    the ORM solutions currently available for .NET. In other words, if all you know
                    is one ORM and one only, then you won't have much of a choice in there,
                    you always &lt;i&gt;have to&lt;/i&gt; go with the only framework that you know of and sometimes
                    it might &lt;i&gt;not&lt;/i&gt; be the best solution for your specific project. Entity Framework
                    could be a viable option, an approach to persistence, proposes to provide a ORM
                    solution with the full commercial support and backing of Microsoft. But is that
                    really enough reason for us to simply ignore just about everything else?
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;table border="0" cellSpacing="0" cellPadding="0" width="100%"&gt;
                        &lt;tbody&gt;
&lt;tr class="hr"&gt;
                            
&lt;td width="100%"&gt;
                                &lt;h3&gt;
                                    What is NHibernate and Why Should You Care?
                                &lt;/h3&gt;
                            &lt;/td&gt;
                            
&lt;td&gt;
                                &lt;img style="width: 200px;" src="http://weblogs.asp.net/blogs/manavi/14/NHLogoSmall.gif" mce_src="http://weblogs.asp.net/blogs/manavi/14/NHLogoSmall.gif"&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;&lt;/table&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;a href="http://nhforge.org/Default.aspx" target="_blank" mce_href="http://nhforge.org/Default.aspx"&gt;NHibernate&lt;/a&gt; is an ambitious
                    open source project that aims to be a complete solution to the problem of managing
                    persistent data in .NET. NHibernate is a .NET port of the very popular and successful
                    Java &lt;a href="http://www.hibernate.org/" target="_blank" mce_href="http://www.hibernate.org/"&gt;Hibernate&lt;/a&gt; library and
                    is one of the most mature, powerful ORM frameworks available in .NET today, an important
                    option and one that you shouldn't ignore.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        What is the Goal of This Post?&lt;/h3&gt;
                    From my experience with the .NET user community, I know that the first thing many
                    developers (especially those with a history of using MS tools and frameworks like
                    EF) say when it comes to NHibernate is that it's a sophisticated framework and has
                    a steep learning curve. In this series of posts I'll walk you through the process
                    of building a project with NHibernate and will show how you can leverage your EF
                    knowledge to get up and running with NH as quickly and easily as possible.
                    &lt;br&gt;
                    &lt;br&gt;
                    This writing is &lt;i&gt;not&lt;/i&gt; a debate on EF vs. NHibernate, nor is a comparison to
                    show which one is better than the other. I'll just present the advantages and drawbacks
                    of the two approaches to you and will show their solutions for various persistence
                    problems. My hope is that at the end it gives you an insight to conclude which one
                    is &lt;i&gt;a better persistence solution for your specific project&lt;/i&gt;.
                    &lt;h3&gt;
                        A Note For Those Who Never Worked with Any ORM Before&lt;/h3&gt;
                    Keep reading! The title of this series might be a bit misleading as I don't assume
                    any knowledge of EF when explaining NHibernate, of course being familiar with one
                    ORM solution like EF would help but is absolutely not a requirement so with a little
                    extra effort, you can learn NHibernate just like the EF developers.
                    &lt;h3&gt;
                        Your First NHibernate Project&lt;/h3&gt;
                    Open your Visual Studio and create a new Console Application project. Before you
                    can start coding your first NHibernate application, you need to reference NHibernate
                    binaries. NHibernate 3.1.0.GA can be downloaded from &lt;a href="http://sourceforge.net/projects/nhibernate/files/NHibernate/3.1.0.GA/NHibernate-3.1.0.GA-bin.zip/download" target="_blank" mce_href="http://sourceforge.net/projects/nhibernate/files/NHibernate/3.1.0.GA/NHibernate-3.1.0.GA-bin.zip/download"&gt;here&lt;/a&gt;. Download and extract the zip file to some location
                    on your drive. The next step is to reference the required binaries in your new project.
                    To do this, follow these steps:
                    
&lt;ol&gt;
                        
&lt;li style="padding-top: 0px;"&gt;Right-click the project and select &lt;i&gt;Add Reference...&lt;/i&gt;&lt;/li&gt;
                        
&lt;li&gt;Click the &lt;i&gt;Browse...&lt;/i&gt; button and navigate to the folder where NHibernate binaries are
                            extracted.&lt;/li&gt;
                        
&lt;li&gt;Open &lt;i&gt;Required_Bins&lt;/i&gt; folder and select &lt;i&gt;NHibernate.dll&lt;/i&gt;. Click OK to add
                            this reference to your project.&lt;/li&gt;
                        
&lt;li&gt;Right-click again on the the project and select &lt;i&gt;Add Reference...&lt;/i&gt; and navigate
                            to the folder where NHibernate binaries are extracted.&lt;/li&gt;
                        
&lt;li&gt;This time open &lt;i&gt;Required_For_LazyLoading&lt;/i&gt; folder and then &lt;i&gt;Castle&lt;/i&gt; folder.&lt;/li&gt;
                        
&lt;li&gt;Add a reference to &lt;i&gt;NHibernate.ByteCode.Castle.dll&lt;/i&gt;.&lt;/li&gt;
                    &lt;/ol&gt;
                    Now that your solution is set up, you’re ready to start coding your first NHibernate
                    application!
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        Creating a Domain Model&lt;/h3&gt;
                    Just like my other posts on EF Code First, this post takes a top-down approach;
                    it assumes that you’re starting with a domain model and trying to derive a new SQL
                    schema but this time with NHibernate. When you’re using NHibernate, entities are implemented as
                    POCOs, a back-to-basics approach that essentially consists of using unbound classes
                    in the business layer which leads to the notion of persistence ignorance in our
                    applications. This results in entities with less coupling that are easier to modify,
                    test, and reuse. EF also supports this pattern starting from its second version
                    (aka EF 4.0).
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 10px; padding-bottom: 10px;" align="center"&gt;
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/14/class_drg.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/14/class_drg.jpg"&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    The following shows the POCO classes that form the object model for this domain:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;User()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Items&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Username&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;&amp;nbsp;Items&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;AddItem(&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;nbsp;item)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;item.Seller&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Items.Add(item);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Steet&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;Item()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Categories&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;ItemId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&amp;nbsp;Price&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;Seller&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&amp;gt;&amp;nbsp;Categories&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;Category()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Items&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;CategoryId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;&amp;nbsp;Items&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;AddItem(&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;nbsp;item)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;item.Categories.Add(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Items.Add(item);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                    
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    The POCO classes are pretty straight forward, except you might wondering what the &lt;i&gt;AddItem&lt;/i&gt;
                    methods are for in there, for now, think of the code in the AddItem (a convenience method)
                    as implementing a strong bidirectional association in the object model.
                    &lt;h3&gt;
                        How Mappings are Specified in NHibernate?&lt;/h3&gt;
                    Just like EF, NHibernate gives you different ways to specify mappings for your persistent
                    classes. You can use XML files, CLR attributes or Fluent API.
                    
&lt;ul&gt;
                        
&lt;li style="padding-top: 6px;"&gt;Mapping using XML&lt;/li&gt;
                        The ability of mapping classes with XML is what NHibernate inherits from Hibernate,
                        where usually each persistent class has an XML mapping file ends with the .hbm.xml
                        extension and is embedded in the same assembly as the mapped class file. This is
                        analogous to an EF's Edmx file but unlike Edmx files, these HBM mapping documents
                        are lightweight, human readable, easily hand-editable and can be easily manipulated
                        by version-control systems. Although Visual Studio comes with a designer support
                        for Edmx files, but the problem is that this support is somewhat limited and you
                        sometimes have to drill down to the underlying XML file to define your desired mapping
                        or behavior, examples like creating a &lt;a href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx"&gt;TPC&lt;/a&gt; mapping or defining &lt;a href="http://weblogs.asp.net/manavi/archive/2011/10/02/nhibernate-for-entity-framework-developers-part-1-fluent-nhibernate.aspx#QueryView" mce_href="http://weblogs.asp.net/manavi/archive/2011/10/02/nhibernate-for-entity-framework-developers-part-1-fluent-nhibernate.aspx#QueryView"&gt;QueryViews&lt;/a&gt;. I personally
                        didn't find editing the raw XMLs in Edmx files (and dealing with an Edmx file in
                        general) a fun practice and I think this is a common feeling throughout the community
                        as we see more and more developers are leaning toward &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx" target="_blank" mce_href="http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx"&gt;Code First development&lt;/a&gt; to keep their projects away from an Edmx file.
                        
&lt;li&gt;Attribute-oriented Mapping&lt;/li&gt;
                        Alternatively, you can use CLR attributes defined in the &lt;a href="http://nhforge.org/doc/nh/en/index.html#mapping-attributes" target="_blank" mce_href="http://nhforge.org/doc/nh/en/index.html#mapping-attributes"&gt;NHibernate.Mapping.Attributes&lt;/a&gt; library to provide all the
                        information NHibernate needs to map your classes. This is comparable to the new
                        attributes that the latest release of EF (EF 4.1) provides in &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=VS.103).aspx"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt; namespace which you
                        can use to map your classes when doing EF Code First development except that EF annotations
                        are not complete and you often have to resort to fluent API to do the mappings 
                        (Examples like creating a &lt;a href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx"&gt;TPC&lt;/a&gt; mapping, customizing a &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx"&gt;TPH&lt;/a&gt; mapping, specifying cascade deletes on associations
                        or customizing the mapping for a many-to-many association.).
                        
&lt;li&gt;Fluent Mapping&lt;/li&gt;
                        Fluent mapping is based on a &lt;a href="http://martinfowler.com/bliki/FluentInterface.html" target="_blank" mce_href="http://martinfowler.com/bliki/FluentInterface.html"&gt;Fluent Interface&lt;/a&gt; that allows you to map your entities completely
                        in code, with all the compile-time safety and refactorability that it brings. Just
                        like the fluent API in EF Code First, &lt;a href="http://fluentnhibernate.org/" target="_blank" mce_href="http://fluentnhibernate.org/"&gt;
                            Fluent NHibernate&lt;/a&gt; project provides an alternative to NHibernate's standard
                        XML mapping files. As a result, rather than writing XML documents, you write mappings
                        in strongly typed C# (or VB) code. It's even has a concept called &lt;a href="http://wiki.fluentnhibernate.org/Auto_mapping" target="_blank" mce_href="http://wiki.fluentnhibernate.org/Auto_mapping"&gt;Auto Mapping&lt;/a&gt; which is a mechanism for automatically mapping
                        all your entities based on a set of conventions. This is very similar to the EF
                        Code First as they both follow the &lt;a href="http://en.wikipedia.org/wiki/Convention_over_Configuration" target="_blank" mce_href="http://en.wikipedia.org/wiki/Convention_over_Configuration"&gt;convention over configuration&lt;/a&gt; principle. In this post we
                        use Fluent NHibernate to map our persistent classes since this allows for easy refactoring,
                        improved readability and more concise and elegant code. More importantly, if you
                        used Code First fluent API before, you'll find this approach very familiar.
                    &lt;/ul&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;table border="0" cellSpacing="0" cellPadding="0" width="100%"&gt;
                        &lt;tbody&gt;
&lt;tr class="hr"&gt;
                            
&lt;td width="100%"&gt;
                                &lt;h3&gt;
                                    Adding Fluent NHibernate to the Project
                                &lt;/h3&gt;
                            &lt;/td&gt;
                            
&lt;td&gt;
                                &lt;img style="width: 200px;" src="http://weblogs.asp.net/blogs/manavi/14/fn_logo.png" mce_src="http://weblogs.asp.net/blogs/manavi/14/fn_logo.png"&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;/tbody&gt;&lt;/table&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    Fluent NHibernate is a separate open source project that needs to be referenced
                    in your project before you can start using its fluent interfaces to map your persistent
                    classes. You can download the binaries from &lt;a href="http://fluentnhibernate.org/downloads/releases/fluentnhibernate-NH3.1-1.2.zip" target="_blank" mce_href="http://fluentnhibernate.org/downloads/releases/fluentnhibernate-NH3.1-1.2.zip"&gt;here&lt;/a&gt;. Once you've got a copy on your local machine, right-click
                    the project and select &lt;i&gt;Add Reference...&lt;/i&gt; and then reference the &lt;i&gt;FluentNHibernate.dll&lt;/i&gt;
                    assembly which is inside the extracted folder from the zip file you downloaded.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        Mapping Entities and Components&lt;/h3&gt;
                    In EF Code First, we use &lt;a href="http://msdn.microsoft.com/en-us/library/gg696117(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/gg696117(v=VS.103).aspx"&gt;EntityTypeConfiguration&lt;/a&gt; class to define a mapping for an entity.
                    We derive from this class to create a mapping, and use its constructor to control
                    how our entity is persisted. Fluent NHibernate has the same approach except that
                    it differs between an entity and a &lt;a href="http://martinfowler.com/eaaCatalog/valueObject.html" mce_href="http://martinfowler.com/eaaCatalog/valueObject.html"&gt;
                        value object&lt;/a&gt; in essence that if you are mapping an entity you'd use &lt;a href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClassMap%601.htm" mce_href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClassMap%601.htm"&gt;
                            ClassMap&lt;/a&gt; and if you are mapping a value object you'd use &lt;a href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ComponentMap%601.htm" mce_href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ComponentMap%601.htm"&gt;
                                ComponentMap&lt;/a&gt; class. The reason for this naming is because NHibernate
                    uses the term &lt;i&gt;component&lt;/i&gt; for a user-defined class that is persisted to the
                    same table as the owning entity. A Component is the exact same concept as a &lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx"&gt;Complex Type&lt;/a&gt; in EF: an object that has no individual identity. The following code shows how to 
                        register the Address class as a component as well as how to specify UserId as the object identifier:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;UserMap&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ClassMap&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;UserMap()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Id(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.UserId);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Component(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3 style="padding-top: 0px;"&gt;
                        Associations in Entity Framework and NHibernate&lt;/h3&gt;
                    As you may know, foreign keys in the first release of EF were not exposed on entities, something
                    we know by the the name of &lt;i&gt;independent associations&lt;/i&gt;. These associations were
                    managed which means they had their own independent entry in the ObjectStateManager
                    where each entry (of course) has its own EntityState. These independent associations
                    make a few things such as N-Tier and concurrency really difficult. For example,
                    concurrency checks were performed on relationships independently of the concurrency
                    checks performed on entities, and there was no way to opt out of these relationship
                    concurrency checks. The result was that your services must carry the original values
                    of relationships and set them on the context before changing relationships. Because
                    of this and a few other problems, these associations eventually became deprecated
                    in the second release of EF and a new type of association called &lt;i&gt;foreign key associations&lt;/i&gt;
                    has been introduced where foreign keys were exposed in the persistent classes along
                    with their related object references (aka navigation properties). The concurrency
                    problem has been solved because now the relationship is simply a property of the
                    entity, and if the entity passes its concurrency check, no further check is needed.
                    Now you can change a relationship just by changing the foreign key value.
                    &lt;br&gt;
                    &lt;br&gt;
                    I have mixed feelings about these foreign key associations. On the one hand, it's
                    not managed anymore and doesn’t expose us to all the problems of independent associations
                    but on the other hand, foreign keys are part of 
                    the &lt;a href="http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch" target="_blank" mce_href="http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch"&gt;impedance mismatch&lt;/a&gt;
                    problem and
                    should not be in the object model. They add noise to the persistence classes and
                    they even introduce some new problems. For example, having foreign keys as well
                    as object references for relationship navigation presents the problem of two different
                    artifacts representing one relationship – this introduces complexity and now you have
                    to make sure that you keep these two in sync.
                    &lt;br&gt;
                    &lt;br&gt;
                    On the other hand, the NHibernate association model is interesting.
                    Foreign keys correctly don't need to be part of the entities but at the same time
                    NHibernate doesn’t "manage" persistent associations. It's kind of the best of two worlds
                    approach. In NHibernate, if you want to manipulate an association, you must write
                    exactly the same code you would write without NHibernate. If an association is bidirectional,
                    both sides of the relationship must be considered. As you can see in the model,
                    the Employee class, for example, doesn't reflect the ProductId foreign key from
                    the database. Next, you'll see how associations are mapped in NHibernate.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        Associations in NHibernate are Directional&lt;/h3&gt;
                    It’s important to understand that NHibernate associations are all inherently unidirectional.
                    As far as NHibernate is concerned, the association from Item to User is a different
                    association than the association from User to Item. To EF developers, this might
                    seems strange; I’ll merely observe that this decision was made because unlike EF,
                    NHibernate objects aren’t bound to any context. In NHibernate applications, the
                    behavior of a nonpersistent instance is the same as the behavior of a persistent
                    instance.
                    &lt;br&gt;
                    &lt;br&gt;
                    Because associations in NHibernate are directional, we call the association from
                    Item to User a &lt;i&gt;many-to-one&lt;/i&gt; association (many Items sold by one user, hence
                    many-to-one). You can now conclude that the inverse association from User to Item
                    is a &lt;i&gt;one-to-many&lt;/i&gt; association. The following shows the mapping code with Fluent NHibrenate for the Item entity:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ItemMap&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ClassMap&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;ItemMap()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Id(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.ItemId);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Name);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Price);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;References(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Seller)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Column(&lt;span style="color: rgb(163, 21, 21);"&gt;"UserId"&lt;/span&gt;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Not.Nullable();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    The &lt;a href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClasslikeMapBase%601.htm#References``1" mce_href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClasslikeMapBase%601.htm#References``1"&gt;
                        References&lt;/a&gt; method creates a many-to-one relationship between two entities.
                    So far we have created a &lt;i&gt;unidirectional many-to-one association&lt;/i&gt;. The column
                    &lt;i&gt;UserId&lt;/i&gt; in the Item table is a foreign key to the primary key of the User
                    table.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        Making the Association Bidirectional&lt;/h3&gt;
                        Now let's have a look at the mapping for the User entity as the other end of the User-Item association. 
                        Here we use the 
                     &lt;a href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClasslikeMapBase%601.htm#HasMany``2" mce_href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClasslikeMapBase%601.htm#HasMany``2"&gt;
                        HasMany&lt;/a&gt; method to create a one-to-many association from User to Item:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;UserMap&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ClassMap&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;UserMap()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Id(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.UserId);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.FullName).Length(50);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.UserName).Not.Nullable().Unique();
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Component(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HasMany(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Items)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Cascade.All()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Inverse()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.KeyColumn(&lt;span style="color: rgb(163, 21, 21);"&gt;"UserId"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    The mapping defined by the &lt;i&gt;KeyColumn&lt;/i&gt; method is a foreign-key column of the
                    associated Item table. Note that we specified the same foreign-key column name in this
                    collection mapping as we specified in the mapping for the many-to-one association.
                    Now we have two different unidirectional associations mapped to the same foreign
                    key. At this point, all we need to do is to tell NHibernate to treat this as a bidirectional
                    association. The &lt;i&gt;Inverse&lt;/i&gt; method tells NHibernate that the collection is a
                    mirror image of the many-to-one association on the other side and we are done.
                    &lt;h3&gt;
                        Mapping a Many-to-Many Association&lt;/h3&gt;                        
                        &lt;a href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClasslikeMapBase%601.htm#HasManyToMany``2" target="_blank" mce_href="http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClasslikeMapBase%601.htm#HasManyToMany``2"&gt;HasManyToMany&lt;/a&gt; method 
                    creates a many-to-many relationship and we use it to map the association between Item and Category entities.
                    In NHiberbate, a bidirectional association always requires that you set both
                    ends of the association and a many-to-many association is &lt;i&gt;not&lt;/i&gt; an exception:
                    
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 5px;"&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CategoryMap&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ClassMap&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;CategoryMap()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Id(c&amp;nbsp;=&amp;gt;&amp;nbsp;c.CategoryId);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(c&amp;nbsp;=&amp;gt;&amp;nbsp;c.Name);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HasManyToMany(c&amp;nbsp;=&amp;gt;&amp;nbsp;c.Items)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Cascade.All()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Table(&lt;span style="color: rgb(163, 21, 21);"&gt;"ItemCategory"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ItemMap&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ClassMap&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;ItemMap()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Id(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.ItemId);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(p&amp;nbsp;=&amp;gt;&amp;nbsp;p.Name);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Map(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Price);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;References(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Seller)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Column(&lt;span style="color: rgb(163, 21, 21);"&gt;"UserId"&lt;/span&gt;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Not.Nullable();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;HasManyToMany(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Categories)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Cascade.All()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Inverse()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Table(&lt;span style="color: rgb(163, 21, 21);"&gt;"ItemCategory"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    When you map a bidirectional many-to-many association, you must declare one end
                    of the association using &lt;i&gt;Inverse&lt;/i&gt; method to define which side’s state is used
                    to update the link table. I chose the Item entity to be the inversed end of this
                    many-to-many association. This setting tells NHibernate to ignore changes made to
                    the Categories collection and use the other end of the association— the items collection —as
                    the representation that should be synchronized with the database.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;NHibernate Configuration and Schema Generation&lt;/h3&gt;
                    The object model and its mappings are ready and now all we need to do is to wire them 
                    to NHibernate. 
                    Fluent NH can help us with this as well since it provides an API for completely configuring NHibernate for use
                    with your application, all in code. The following shows how:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 10px;"&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionFactory&lt;/span&gt;&amp;nbsp;factory&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Fluently&lt;/span&gt;.Configure()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Database(&lt;span style="color: rgb(43, 145, 175);"&gt;MsSqlConfiguration&lt;/span&gt;.MsSql2008
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ConnectionString(c&amp;nbsp;=&amp;gt;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.FromConnectionStringWithKey(&lt;span style="color: rgb(163, 21, 21);"&gt;"SQLExpress"&lt;/span&gt;)))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Mappings(m&amp;nbsp;=&amp;gt;&amp;nbsp;m.FluentMappings.AddFromAssemblyOf&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Program&lt;/span&gt;&amp;gt;())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ExposeConfiguration(config&amp;nbsp;=&amp;gt;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;SchemaExport&lt;/span&gt;&amp;nbsp;schemaExport&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;SchemaExport&lt;/span&gt;(config);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;schemaExport.Drop(&lt;span style="color: blue;"&gt;true&lt;/span&gt;,&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;schemaExport.Create(&lt;span style="color: blue;"&gt;true&lt;/span&gt;,&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;})
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.BuildSessionFactory();&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 10px;"&gt;
                    &lt;i&gt;Fluently.Configure&lt;/i&gt; starts the configuration process. The &lt;i&gt;Database&lt;/i&gt; method is where you specify
                    your database configuration using the &lt;a href="http://wiki.fluentnhibernate.org/Database_configuration" target="_blank" mce_href="http://wiki.fluentnhibernate.org/Database_configuration"&gt;database configuration API&lt;/a&gt;. &lt;i&gt;ExposeConfiguration&lt;/i&gt; is optional, but allows you to alter
                    the raw Configuration object. &lt;i&gt;BuildSessionFactory&lt;/i&gt; is the final call, and it creates
                    the NHibernate &lt;i&gt;SessionFactory&lt;/i&gt; instance from your configuration. We'll take a closer look at ISessionFactory and other NHibernate's 
                    core interfaces in the next post.
                    &lt;br&gt;
                    &lt;br&gt;
                    As you may have noticed, this code also generates the DDL from the object model and executes it against the target database but with one caveat:        
                    You need to manually create the database yourself before running this code since NHibernate will &lt;i&gt;not&lt;/i&gt; create the database for you.
                    Like the class name suggests (SchemaExport), it only creates the schemas in the database. After creating the database, you can run this code 
                    as many times as you want, as it is designed just like EF Code First's &lt;a href="http://msdn.microsoft.com/en-us/library/gg679506(v=VS.103).aspx" mce_href="http://msdn.microsoft.com/en-us/library/gg679506(v=VS.103).aspx"&gt;DropCreateDatabaseAlways&lt;/a&gt;  
                    strategy.
                    &lt;h3&gt;
                        SQL Schema&lt;/h3&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    The presented object model will result in the creation of the following schema in the database:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 15px;" align="center"&gt;
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/14/schema.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/14/schema.jpg"&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;Source Code&lt;/h3&gt;
                    Click &lt;a href="http://weblogs.asp.net/blogs/manavi/14/NHibernate.Sample.Part1.zip" mce_href="http://weblogs.asp.net/blogs/manavi/14/NHibernate.Sample.Part1.zip"&gt;here&lt;/a&gt; to download the source code for the project that we have built in this post.
                &lt;/td&gt;
            &lt;/tr&gt;
            
            
&lt;tr&gt;
                
&lt;td style="padding-bottom: 30px;"&gt;
                    &lt;h3&gt;Summary&lt;/h3&gt;
                    In this post, we take a high-level look at NHibernate and its mapping system by running
                    a simple example. We also saw how to configure NHibernate with Fluent NHibernate, a technique similar to EF Fluent API which 
                    saves us from dealing with XML mapping files. This post sets the basis for the next part where we will focus more on 
                    the dynamic behavioral aspects of NHibernate, something that comes into play at runtime and we'll see how it differs from EF
                    in that area.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-bottom: 30px;" class="footNote"&gt;
                    &lt;a title="QueryView" name="QueryView"&gt;&lt;/a&gt;
                    &lt;span class="link"&gt;*&lt;/span&gt; A QueryView is a mapping that allows you to override the default mapping for an
                    entity and return a new read-only strong type. This way, you can get the benefit
                    of a projection, but return an entity instead of an anonymous type which you can’t
                    pass around from one method to another.
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;style type="text/css"&gt;
        a, .link
        {
            color: #5A99FF;
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3, hr
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;
            line-height: 22px;
            padding-top: 10px; /*margin: 33px 0 11px;*/
        }
        
        .footNote
        {
            font-size: 11px;
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
        
        .subTitle
        {
            font-weight: bold;
            padding-top: 15px;
            padding-bottom: 5px;
        }
        
        td
        {
            line-height: 18px;
        }
        
        ul &gt; li
        {
            padding-top: 20px;
            padding-bottom: 5px;
            font-weight: bold;
        }
        
        ol &gt; li
        {
            padding-top: 5px;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .exception
        {
            background-color: #f0f0f0;
            font-style: italic;
            font-family: Segoe UI;
            padding-bottom: 5px;
            padding-left: 10px;
            padding-top: 5px;
            padding-right: 5px;
        }
        
        .myComment
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
    &lt;/style&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7972560" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="Fluent NHibernate" scheme="http://weblogs.asp.net/manavi/archive/tags/Fluent+NHibernate/default.aspx" /><category term="NHibernate" scheme="http://weblogs.asp.net/manavi/archive/tags/NHibernate/default.aspx" /></entry><entry><title>Associations in EF Code First: Part 6 – Many-valued Associations</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx</id><published>2011-05-17T04:16:00Z</published><updated>2011-05-17T04:16:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
        &lt;tbody&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    This is the sixth and last post in a series that explains entity association mappings with
                    EF Code First. I've described these association types so far:
                    
&lt;ul&gt;
                        
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx"&gt;
                            Part 1 – Introduction and Basic Concepts&lt;/a&gt;&lt;/li&gt;
                        
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;Part 2 – Complex Types&lt;/a&gt;&lt;/li&gt;
                        
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;Part 3 – Shared Primary Key Associations&lt;/a&gt;&lt;/li&gt;
                        
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" target="_blank"&gt;Part 4 – Table Splitting&lt;/a&gt;&lt;/li&gt;
                        
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" target="_blank"&gt;Part 5 – One-to-One Foreign Key Associations&lt;/a&gt;&lt;/li&gt;
                        
&lt;li&gt;Part 6 – Many-valued Associations&lt;/li&gt;
                    &lt;/ul&gt;
                    Support for many-valued associations is an absolutely basic feature of an ORM solution
                    like Entity Framework. Surprisingly, we’ve managed to get this far without needing
                    to talk much about these types of associations. Even more surprisingly, there is
                    not much to say on the topic—these associations are so easy to use in EF that we
                    don’t need to spend a lot of effort explaining it. To get an overview, we first
                    consider a domain model containing different types of associations and will provide 
                    necessary explanations around each of them. Since this is the last post in this series, 
                    I'll show you two tricks at the end of this post that you might find them useful in your EF Code First developments.                    
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;Many-valued entity associations&lt;/h3&gt;
                    A many-valued entity association is by definition a collection of entity references.
                    One-to-many associations are the most important kind of entity association that
                    involves a collection. We go so far as to discourage the use of more exotic association
                    styles when a simple bidirectional many-to-one/one-to-many will do the job. A many-to-many
                    association may always be represented as two many-to-one associations to an intervening
                    class. This model is usually more easily extensible, so we tend not to use many-to-many
                    associations in applications.
                    &lt;h3&gt;
                        Introducing the OnlineAuction Domain Model&lt;/h3&gt;
                    The model we introducing here is related to an online auction system. OnlineAuction site auctions many different kinds of items. Auctions proceed
                    according to the “English auction” model: users continue to place bids on an item
                    until the bid period for that item expires, and the highest bidder wins. A high-level
                    overview of the domain model is shown in the following class diagram:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 15px; padding-bottom: 15px;" align="center"&gt;
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/13/domain_model.jpg"&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    Each item may be auctioned only once, so we have a single auction item entity named
                    Item. Bid is associated directly with Item.
                    &lt;h3&gt;
                        The Object Model&lt;/h3&gt;
                    The following shows the POCO classes that form the object model for this domain:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;&amp;nbsp;BoughtItems&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;ItemId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&amp;nbsp;InitialPrice&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;&amp;nbsp;StartDate&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;&amp;nbsp;EndDate&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?&amp;nbsp;BuyerId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?&amp;nbsp;SuccessfulBidId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;Buyer&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Bid&lt;/span&gt;&amp;nbsp;SuccessfulBid&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Bid&lt;/span&gt;&amp;gt;&amp;nbsp;Bids&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&amp;gt;&amp;nbsp;Categories&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Bid&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BidId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;double&lt;/span&gt;&amp;nbsp;Amount&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;&amp;nbsp;CreatedOn&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;ItemId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BidderId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;nbsp;Item&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;Bidder&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;CategoryId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?&amp;nbsp;ParentCategoryId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&amp;nbsp;ParentCategory&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Category&lt;/span&gt;&amp;gt;&amp;nbsp;ChildCategories&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ICollection&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;&amp;nbsp;Items&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        The Simplest Possible Association&lt;/h3&gt;
                    The association from Bid to Item (and vice versa) is an example of the simplest
                    possible kind of entity association. You have two properties in two classes. One
                    is a collection of references, and the other a single reference. This mapping is
                    called a &lt;i&gt;bidirectional one-to-many association&lt;/i&gt;. The property ItemId in the
                    Bid class is a foreign key to the primary key of the Item entity, something that
                    we call a &lt;a href="http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx" target="_blank"&gt;&lt;i&gt;Foreign Key Association&lt;/i&gt;&lt;/a&gt; in EF 4. We defined the type of the ItemId
                    property as an int which can't be null because we can’t have a bid without an item—a
                    constraint will be generated in the SQL DDL to reflect this. We use &lt;a href="http://msdn.microsoft.com/en-us/library/gg671317(v=VS.103).aspx" target="_blank"&gt;HasRequired&lt;/a&gt; method in fluent API to create this type of
                    association:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BidConfiguration&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EntityTypeConfiguration&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Bid&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;internal&lt;/span&gt;&amp;nbsp;BidConfiguration()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.HasRequired(b&amp;nbsp;=&amp;gt;&amp;nbsp;b.Item)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Bids)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(b&amp;nbsp;=&amp;gt;&amp;nbsp;b.ItemId);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        An Optional One-to-Many Association Between User and Item Entities&lt;/h3&gt;
                    Each item in the auction may be bought by a User, or might not be sold at all. Note
                    that the foreign key property BuyerId in the Item class is of type &lt;a href="http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx" target="_blank"&gt;Nullable&amp;lt;int&amp;gt;&lt;/a&gt; which can be NULL as the association is
                    in fact to-zero-or-one. We use &lt;a href="http://msdn.microsoft.com/en-us/library/gg671230(v=VS.103).aspx" target="_blank"&gt;HasOptional&lt;/a&gt; method to create this association between User
                    and Item (using this method, the foreign key must be a Nullable type or Code First
                    throws an exception):
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ItemConfiguration&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EntityTypeConfiguration&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;internal&lt;/span&gt;&amp;nbsp;ItemConfiguration()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.HasOptional(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Buyer)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BoughtItems)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.BuyerId);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        A Parent/Child Relationship&lt;/h3&gt;
                    In the object model, the association between User and Item is fairly loose. We’d
                    use this mapping in a real system if both entities had their own lifecycle and were
                    created and removed in unrelated business processes. Certain associations are much
                    stronger than this; some entities are bound together so that their lifecycles aren’t
                    truly independent. For example, it seems reasonable that deletion of an item
                    implies deletion of all bids for the item. A particular bid instance references
                    only one item instance for its entire lifetime. In this case, cascading deletions
                    makes sense. In fact, this is what the composition (the filled out diamond) in the
                    above UML diagram means. If you enable cascading delete, the association between
                    Item and Bid is called a &lt;i&gt;parent/child relationship&lt;/i&gt;, and that's exactly what
                    EF Code First does by default on associations created with the HasRequired method.
                    &lt;br&gt;
                    &lt;br&gt;
                    In a parent/child relationship, the parent entity is responsible for the lifecycle
                    of its associated child entities. This is the same semantic as a composition using
                    EF &lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;complex types&lt;/a&gt;, but in this case only entities are involved;
                    Bid isn’t a value type. The advantage of using a parent/child relationship is that
                    the child may be loaded individually or referenced directly by another entity. A
                    bid, for example, may be loaded and manipulated without retrieving the owning item.
                    It may be stored without storing the owning item at the same time. Furthermore,
                    you reference the same Bid instance in a second property of Item, the single SuccessfulBid
                    (take another look at the Item class in the object model above). Objects of value
                    type can’t be shared.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        Many-to-Many Associations&lt;/h3&gt;
                    The association between Category and Item is a many-to-many association, as can
                    be seen in the above class diagram. a many-to-many association mapping hides the
                    intermediate association table from the application, so you don’t end up with an
                    unwanted entity in your domain model. That said, In a real system, you may not have
                    a many-to-many association since my experience is that there is almost always other
                    information that must be attached to each link between associated instances (such
                    as the date and time when an item was added to a category) and that the best way
                    to represent this information is via an intermediate association class (In EF, you
                    can map the association class as an entity and map two one-to-many associations
                    for either side.). 
                    &lt;br&gt;
                    &lt;br&gt;
                    In a many-to-many relationship, the join table (or link table,
                    as some developers call it) has two columns: the foreign keys of the Category and
                    Item tables. The primary key is a composite of both columns. In EF Code First, many-to-many
                    associations mappings can be customized with a fluent API code like this:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ItemConfiguration&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EntityTypeConfiguration&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Item&lt;/span&gt;&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;internal&lt;/span&gt;&amp;nbsp;ItemConfiguration()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.HasMany(i&amp;nbsp;=&amp;gt;&amp;nbsp;i.Categories)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany(c&amp;nbsp;=&amp;gt;&amp;nbsp;c.Items)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Map(mc&amp;nbsp;=&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mc.MapLeftKey(&lt;span style="color: rgb(163, 21, 21);"&gt;"ItemId"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mc.MapRightKey(&lt;span style="color: rgb(163, 21, 21);"&gt;"CategoryId"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mc.ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"ItemCategory"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        SQL Schema&lt;/h3&gt;
                    The following shows the SQL schema that Code First creates from our object model:
                    &lt;!--&lt;h3&gt;Applying filters when explicitly loading related entities&lt;/h3&gt;--&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td align="center"&gt;
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/13/sql_schema.jpg"&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;Get the Code First Generated SQL DDL&lt;/h3&gt;
                    A common process, if you’re starting with a new application and new database,
                    is to generate DDL with Code First automatically during development; At the same time (or later, during testing), 
                    a professional DBA verifies and optimizes the SQL DDL and creates the final database schema. You can export the
                    DDL into a text file and hand it to your DBA. 
                    &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.createdatabasescript.aspx" target="_blank"&gt;CreateDatabaseScript&lt;/a&gt;
                    on ObjectContext class generates a data definition language (DDL) script that 
                    creates schema objects (tables, primary keys, foreign keys) for the metadata in the the store schema definition language (SSDL) file (in the next section, you'll see where this metadata come from): 
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;script&amp;nbsp;=&amp;nbsp;((&lt;span style="color: rgb(43, 145, 175);"&gt;IObjectContextAdapter&lt;/span&gt;)context).ObjectContext.CreateDatabaseScript();
}&lt;/pre&gt;
        
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;                 
                You can then use one of the classes in the .Net File IO API like 
                &lt;a href="http://msdn.microsoft.com/en-us/library/3ssew6tk.aspx" target="_blank"&gt;StreamWriter&lt;/a&gt;
                to write the script on the disk.                    
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-top: 20px; padding-bottom: 20px;"&gt;                 
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/13/sql_ddl.png"&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                    Note how Code First enables cascade deletes for the parent/child relationship between Item and Bid
                    &lt;h3&gt;Get the Runtime EDM&lt;/h3&gt;
                    One of the benefits of Code First development is that we don't need to deal with the Edmx file, however, that doesn't 
                    mean that the concept of EDM doesn't exist at all. In fact, at runtime, when
                    the context is used for the first time, Code First derives the EDM (CSDL, MSL, and SSDL) from our object model 
                    and this EDM is even
                    &lt;a href="http://blog.oneunicorn.com/2011/04/15/code-first-inside-dbcontext-initialization" target="_blank"&gt;cached in the app-domain&lt;/a&gt;
                    as an instance of DbCompiledModel. 
                    Having access to this generated EDM is beneficial in many cases. At the very least, we can add it to our solution and use it as a 
                    class diagram for our domain model. More importantly, 
                    we can use this EDM 
                    for debugging when there is a need to look at the model that Code First creates internally.
                    This EDM also contains the conceptual schema definition language (CSDL) something that drives the EF runtime behavior.
                    The trick is to use the 
                    &lt;a href="http://msdn.microsoft.com/en-us/library/gg696608(v=VS.103).aspx" target="_blank"&gt;WriteEdmx&lt;/a&gt;
                    Method from the 
                    &lt;a href="http://msdn.microsoft.com/en-us/library/gg696363(v=VS.103).aspx" target="_blank"&gt;EdmxWriter&lt;/a&gt;
                    class like the following code:
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;                 
                    
                    
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;XmlWriterSettings&lt;/span&gt;&amp;nbsp;settings&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;XmlWriterSettings&lt;/span&gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;settings.Indent&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;true&lt;/span&gt;;
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: rgb(43, 145, 175);"&gt;XmlWriter&lt;/span&gt;&amp;nbsp;writer&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;XmlWriter&lt;/span&gt;.Create(&lt;span style="color: rgb(163, 21, 21);"&gt;@"Model.edmx"&lt;/span&gt;,&amp;nbsp;settings))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EdmxWriter&lt;/span&gt;.WriteEdmx(context,&amp;nbsp;writer);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/pre&gt;
                    
                &lt;/td&gt;
            &lt;/tr&gt;
            
            
&lt;tr&gt;
                
&lt;td&gt;
                 After running this code, simply right click on your project and select &lt;i&gt;Add Existing Item...&lt;/i&gt;
                and then browse and add the Model.edmx file to the project. Once you added the file, double click on 
                it and visual studio will perfectly show the edmx file in the designer:                                
                &lt;/td&gt;
            &lt;/tr&gt;
            
            
&lt;tr&gt;
                
&lt;td style="padding-top: 20px; padding-bottom: 20px;"&gt;
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/13/edmx_prop.png"&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td&gt;
                Also note how cascade delete is also enabled in the CSDL for the parent/child association between Item and Bid.
                
                    &lt;h3&gt;
                        Source Code&lt;/h3&gt;
                    Click &lt;a href="http://weblogs.asp.net/blogs/manavi/13/OnlineAuction.zip"&gt;here&lt;/a&gt; to
                    download the source code for the OnlineAuction site that
                    we have seen in this post.
                &lt;/td&gt;
            &lt;/tr&gt;
            
&lt;tr&gt;
                
&lt;td style="padding-bottom: 30px;"&gt;
                    &lt;h3&gt;Summary&lt;/h3&gt;
                    In this series, we focused on the structural aspect of the object/relational paradigm
mismatch and discussed one of the main ORM problems relating to associations. We explored the programming
model for persistent classes and the EF Code First fluent API for fine-grained
classes and associations. Many of the techniques we’ve shown
in this series are key concepts of object/relational mapping and I am hoping that you'll find them useful in your Code First developments.
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;style type="text/css"&gt;
        a
        {
            color: #5A99FF;
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;
            line-height: 22px;
            padding-top: 10px; /*margin: 33px 0 11px;*/
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
        
        .subTitle
        {
            font-weight: bold;
            padding-top: 15px;
            padding-bottom: 5px;
        }
        
        td
        {
            line-height: 18px;
        }
        
        li
        {
            padding-top: 5px;
            padding-bottom: 5px;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .exception
        {
            background-color: #f0f0f0;
            font-style: italic;
            font-family: Segoe UI;
            padding-bottom: 5px;
            padding-left: 10px;
            padding-top: 5px;
            padding-right: 5px;
        }
        
        .myComment
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
    &lt;/style&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7793869" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="Entity Framework 4.1" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework+4.1/default.aspx" /></entry><entry><title>Associations in EF Code First: Part 5 – One-to-One Foreign Key Associations</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx</id><published>2011-05-01T17:35:00Z</published><updated>2011-05-01T17:35:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;This is the fourth post in a series that explains entity association mappings with EF Code First. I've described these association types so far: 
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx"&gt;Part 1 – Introduction and Basic Concepts&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;Part 2 – Complex Types&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;Part 3 – Shared Primary Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" target="_blank"&gt;Part 4 – Table Splitting&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Part 5 – One-to-One Foreign Key Associations&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx" target="_blank"&gt;Part 6 – Many-valued Associations&lt;/a&gt;&lt;/li&gt;


&lt;/ul&gt;
In the &lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;third part&lt;/a&gt; of this series we saw the limitations of shared primary key association and argued that this type of association is relatively rare and in many schemas, a one-to-one association is represented with a foreign key field and a unique constraint. Today we are going to discuss how this is done by learning about one-to-one foreign key associations. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Introducing the Revised Model&lt;/h3&gt;In this revised version, each User always have two addresses: one billing address and another one for delivery. The following class diagram demonstrates the domain model: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 20px;" align="center"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/12/classdrg.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;One-to-One Foreign Key Association&lt;/h3&gt;Instead of &lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;sharing a primary key&lt;/a&gt;, two rows can have a foreign key relationship. One table has a foreign key column that references the primary key of the associated table (The source and target of this foreign key constraint can even be the same table: This is called a self-referencing relationship.). &lt;u&gt;An additional constraint enforces this relationship as a real one to one&lt;/u&gt;. For example, by making the BillingAddressId column unique, we declare that a particular address can be referenced by at most one user, as a billing address. This isn’t as strong as the guarantee from a shared primary key association, which allows a particular address to be referenced by at most one user, period. With several foreign key columns (which is the case in our domain model since we also have a foreign key for DeliveryAddress), we can reference the same address target row several times. But in any case, &lt;u&gt;two users can’t share the same address for the same purpose&lt;/u&gt;. 
&lt;h3&gt;The Object Model&lt;/h3&gt;Let's start by creating an object model for our domain: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas; font-size: 13px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingAddressId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;DeliveryAddressId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;BillingAddress&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;DeliveryAddress&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;AddressId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;&amp;nbsp;Users&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;gt;&amp;nbsp;Addresses&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;As you can see, User class has introduced two new scalar properties as &lt;em&gt;BillingAddressId&lt;/em&gt; and &lt;em&gt;DeliveryAddressId&lt;/em&gt; as well as their related navigation properties (&lt;em&gt;BillingAddress &lt;/em&gt;and &lt;em&gt;DeliveryAddress&lt;/em&gt;). 
&lt;h3&gt;Configuring Foreign Keys With Fluent API&lt;/h3&gt;BillingAddressId and DeliveryAddressId are foreign key scalar properties representing the actual foreign key values that the relationships are established on. However, Code First will &lt;u&gt;not&lt;/u&gt; recognize them as the foreign keys for the associations since their names are not aligned with the &lt;a href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx" target="_blank"&gt;conventions&lt;/a&gt; that it has to infer foreign keys. Therefore, we need to use fluent API (or Data Annotations) to let Code First know about the foreign key properties. The following fluent API code shows how: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas; font-size: 13px;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddressId);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddressId);
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Alternatively, we can use Data Annotations to achieve this. EF 4.1 introduced a new attribute in &lt;a href="http://msdn.microsoft.com/en-us/library/cc490428(v=VS.103).aspx" target="_blank"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt; namespace called &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.foreignkeyattribute(v=VS.103).aspx" target="_blank"&gt;ForeignKeyAttribute&lt;/a&gt;. We can place this on a navigation property to specify the property that represents the foreign key of the relationship: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas; font-size: 13px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingAddressId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;DeliveryAddressId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color: rgb(43, 145, 175);"&gt;ForeignKey&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"BillingAddressId"&lt;/span&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;BillingAddress&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color: rgb(43, 145, 175);"&gt;ForeignKey&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"DeliveryAddressId"&lt;/span&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;DeliveryAddress&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;That said, we won't use this data annotation and will go with the fluent API way for a reason that you'll soon see. 
&lt;h3&gt;Creating a SQL Server Schema&lt;/h3&gt;The object model seems to be ready to give us the desired SQL schema, however, if we try to create a SQL Server database from it, we will get an &lt;a href="http://msdn.microsoft.com/en-us/library/system.invalidoperationexception.aspx" target="_blank"&gt;InvalidOperationException&lt;/a&gt; with this message: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px; padding-bottom: 10px;"&gt;
&lt;div style="width: 640px;" class="exception"&gt;The database creation succeeded, but the creation of the database objects did not. See InnerException for details. &lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;The inner exception is a &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=VS.100).aspx" target="_blank"&gt;SqlException&lt;/a&gt; containing this message: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px; padding-bottom: 10px;"&gt;
&lt;div style="width: 640px;" class="exception"&gt;Introducing FOREIGN KEY constraint 'User_DeliveryAddress' on table 'Users' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors. &lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;As you can tell from the type of the inner exception (SqlException), it has nothing to do with EF or Code First; it has been generated purely by SQL Server when Code First was trying to create a database based on our object model. 
&lt;h3&gt;What's a Multiple Cascade Path Anyway?&lt;/h3&gt;A &lt;i&gt;Multiple Cascade Path&lt;/i&gt; happens when a cascade path goes from column col1 in table A to table B and also from column col2 in table A to table B. For example in our case Code First attempted to turn on cascade delete for both BillingAddressId and DeliveryAddressId columns in the Users table. In fact, Code First was trying to use &lt;a href="http://www.nerdymusings.com/LPMArticle.asp?ID=34" target="_blank"&gt;Declarative Referential Integrity (DRI)&lt;/a&gt; to enforce cascade deletes and the problem is that SQL Server is not fully &lt;a href="http://en.wikipedia.org/wiki/SQL-92" target="_blank"&gt;ANSI SQL-92&lt;/a&gt; compliant when it comes to the cascading actions. &lt;u&gt;In SQL Server, DRI forbids cascading updates or deletes in a multiple cascade path scenario&lt;/u&gt;. &lt;br&gt;&lt;br&gt;A &lt;a href="http://support.microsoft.com/kb/321843" target="_blank"&gt;KB article&lt;/a&gt; also explains why we received this error: &lt;br&gt;
&lt;div style="padding-top: 10px; padding-bottom: 10px;"&gt;"In SQL Server, a table cannot appear more than one time in a list of all the cascading referential actions that are started by either a DELETE or an UPDATE statement. For example, the tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree". &lt;/div&gt;And it exactly applies to our example: The User table appeared twice in a list of cascading referential actions started by a DELETE from the Addresses table. Basically, SQL Server does simple counting of cascade paths and, rather than trying to work out whether any cycles actually exist, it assumes the worst and refuses to create the referential actions (cascades). Therefore, depend on your database engine, you may or may not get this exception. 
&lt;h3&gt;Overriding The Code First Convention To Resolve the Problem&lt;/h3&gt;As you saw, Code First automatically turns on cascade delete on a &lt;em&gt;required&lt;/em&gt; one-to-many association based on the &lt;a href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx" target="_blank"&gt;conventions&lt;/a&gt;. However, in order to resolve the exception that we got from SQL Server, we have no choice other than overriding this cascade delete behavior detected by convention. Basically we need to switch cascade delete off on at least one of the relationships and as of EF 4.1, there is no way to accomplish this other than using fluent API. Let's switch it off on DeliveryAddress association for example: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddressId);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddressId).&lt;span style="border-bottom-color: red; border-bottom-width: 1px; border-bottom-style: solid;"&gt;WillCascadeOnDelete(&lt;span style="color: blue;"&gt;false&lt;/span&gt;)&lt;/span&gt;;
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;One-to-One Foreign Key Associations in EF Code First&lt;/h3&gt;As you may have noticed, both associations in the fluent API code has been configured as a &lt;i&gt;many-to-one&lt;/i&gt;—not one-to-one, as you might have expected. The reason is simple: Code First (and EF in general) does &lt;u&gt;not&lt;/u&gt; natively support one-to-one foreign key associations. In fact, EF does not support any association scenario that involves unique constraints at all. Fortunately, in this case we don’t care what’s on the target side of the association, so we can treat it like a &lt;em&gt;to-one&lt;/em&gt; association without the &lt;em&gt;many&lt;/em&gt; part. All we want is to express “This entity (User) has a property that is a reference to an instance of another entity (Address)” and use a foreign key field to represent that relationship. EF (of course) still thinks that the relationship is many-to-one. This is a workaround for the current EF limitation which comes with two consequences: First, EF won't create any additional constraint for us to enforces this relationship as a one to one, we need to manually create it ourselves. The second limitation that this lack of support impose to us is more important: &lt;u&gt;one to one foreign key associations cannot be bidirectional&lt;/u&gt; (e.g. we cannot define a property for the User on the Address class). 
&lt;h3&gt;Create a Unique Constraint To Enforce the Relationship as a One to One&lt;/h3&gt;We can manually create unique constraints on the foreign keys in the database after Code First creates it for us but if you are like me and prefer to create your database in one shot then there is a way to have Code First create the constraints as part of its database creation process. For that we can take advantage of the new EF 4.1 &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.database.executesqlcommand(v=VS.103).aspx" target="_blank"&gt;ExecuteSqlCommand&lt;/a&gt; method on &lt;a href="http://msdn.microsoft.com/en-us/library/gg696292(v=VS.103).aspx" target="_blank"&gt;Database&lt;/a&gt; class which allows raw SQL commands to be executed against the database. The best place to invoke ExecuteSqlCommand method for this purpose is inside a &lt;a href="http://msdn.microsoft.com/en-us/library/gg679212(v=VS.103).aspx" target="_blank"&gt;Seed&lt;/a&gt; method that has been overridden in a custom initializer class: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas; font-size: 13px;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;Seed(&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;&amp;nbsp;context)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Database.ExecuteSqlCommand(&lt;span style="color: rgb(163, 21, 21);"&gt;"ALTER&amp;nbsp;TABLE&amp;nbsp;Users&amp;nbsp;ADD&amp;nbsp;CONSTRAINT&amp;nbsp;uc_Billing&amp;nbsp;UNIQUE(BillingAddressId)"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Database.ExecuteSqlCommand(&lt;span style="color: rgb(163, 21, 21);"&gt;"ALTER&amp;nbsp;TABLE&amp;nbsp;Users&amp;nbsp;ADD&amp;nbsp;CONSTRAINT&amp;nbsp;uc_Delivery&amp;nbsp;UNIQUE(DeliveryAddressId)"&lt;/span&gt;);
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;This code adds unique constraints to the BillingAddressId and DeliveryAddressId columns in the DDL generated by Code First. 
&lt;h3&gt;SQL Schema&lt;/h3&gt;The object model is ready now and will result in the following database schema: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="padTop5"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/12/db_schema.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 12px;"&gt;It is also worth mentioning that we can still enforce cascade deletes for the Delivery Address relationship. SQL Server allows enforcing referential integrity in two different ways. DRI that we just saw is the most basic yet least flexible way. The other way is to use &lt;em&gt;Triggers&lt;/em&gt;. We can write a &lt;a href="http://support.microsoft.com/kb/142480#top" target="_blank"&gt;Delete Trigger&lt;/a&gt; on the primary table that either deletes the rows in the dependent table(s) or sets all corresponding foreign keys to NULL (In our case the foreign keys are Non-Nullable so it has to delete the dependent rows). 
&lt;h3&gt;Source Code&lt;/h3&gt;Click &lt;a href="http://weblogs.asp.net/blogs/manavi/12/OneToOneFK.zip"&gt;here&lt;/a&gt; to download the source code for the one-to-one foreign key association sample that we have built in this post. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Summary&lt;/h3&gt;In this post we learned about one-to-one foreign key associations as a better way to create one to one relationships. We saw some limitations such as the need for manual creation of unique constraints and also the fact that this type of association cannot be bidirectional, all due to the lack of unique constraint support in EF. The good news is that the ADO.NET team is working on &lt;a href="http://blogs.msdn.com/b/efdesign/archive/2011/03/09/unique-constraints-in-the-entity-framework.aspx"&gt;enabling unique constraints&lt;/a&gt; in EF but support for unique constraints requires changes to the whole EF stack which won't happen until the next major release of EF (EF 4.1 is merely layered on top of the current .NET 4.0 functionality) and until then the workaround that I showed here is going to be the way to implement one-to-one foreign key associations in EF Code First. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-bottom: 30px;"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;
        a
        {
            color: #5A99FF;
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;
            line-height: 22px;
            padding-top: 10px; /*margin: 33px 0 11px;*/
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
        
        .subTitle
        {
            font-weight: bold;
            padding-top: 15px;
            padding-bottom: 5px;
        }
        
        td
        {
            line-height: 18px;
        }
        
        li
        {
            padding-top: 5px;
            padding-bottom: 5px;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .exception
        {
            background-color: #f0f0f0;
            font-style: italic;
            font-family: Segoe UI;
            padding-bottom: 5px;
            padding-left: 10px;
            padding-top: 5px;
            padding-right: 5px;
        }
        
        .myComment
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
    &lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7774246" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="Entity Framework 4.1" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework+4.1/default.aspx" /></entry><entry><title>Associations in EF Code First: Part 4 – Table Splitting</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx</id><published>2011-04-24T20:17:00Z</published><updated>2011-04-24T20:17:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;This is the fourth post in a series that explains entity association mappings with EF Code First. This series includes: 
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx"&gt;Part 1 – Introduction and Basic Concepts&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;Part 2 – Complex Types&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;Part 3 – Shared Primary Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Part 4 – Table Splitting&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" target="_blank"&gt;Part 5 – One-to-One Foreign Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx" target="_blank"&gt;Part 6 – Many-valued Associations&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;In the &lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;second part&lt;/a&gt; of this series we saw how to map a special kind of one-to-one association—a composition with complex types. We argued that this is usually the simplest way to represent one-to-one relationships which comes with some &lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx#Limitations" target="_blank"&gt;limitations&lt;/a&gt;. We addressed the first limitation (shared references) by introducing shared primary key associations in the &lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;previous blog post&lt;/a&gt;. In today’s blog post we are going to address the third limitation of the complex types by learning about &lt;i&gt;Table Splitting&lt;/i&gt; as yet another way to map a one-to-one association. 
&lt;h3&gt;The Motivation Behind this Mapping: A Complex Type That Can be Lazy Loaded&lt;/h3&gt;A shared primary key association does not expose us to the third limitation of the complex types regarding &lt;a href="http://martinfowler.com/eaaCatalog/lazyLoad.html" target="_blank"&gt;Lazy Loading&lt;/a&gt;, we can of course lazy/defer load the Address information of a given user but at the same time, it does not give us the same SQL schema as the complex type mapping. After all, it adds a new table for the Address entity to the schema while mapping the Address with a complex type stores the address information in the Users table. So the question still remains there: How can we keep everything (e.g. User and Address) in one single table yet be able to lazy load the complex type part (Address) after reading the principal entity (User)? In other words, how can we have lazy loading with a complex type? 
&lt;h3&gt;Splitting a Single Table into Multiple Entities&lt;/h3&gt;Table splitting (a.k.a. horizontal splitting) enables us to map a single table to multiple entities. This is particularly useful for scenarios that we have a table with many columns where some of those columns might not be needed as frequently as others or some of the columns are expensive to load (e.g. a column with a binary data type). 
&lt;h3&gt;An Example From the Northwind Database&lt;/h3&gt;Unlike the other parts of this series, where we start with an object model and then derive a SQL schema afterwards, in this post we are going to do the reverse, for a reason that you'll see, we will start with an existing schema and will try to create an object model that matches the schema. For that we are going to use the Employees table from the Northwind database. You can download and install Northwind database from &lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034&amp;amp;displaylang=en" target="_blank"&gt;here&lt;/a&gt; If you don't have it already installed on your SQL Server. The following shows the Employees table from the Northwind database that we are going to use: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 25px; padding-bottom: 15px;" align="center"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/11/EmployeesTable.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;As you can see, this table has a Photo column of image type which makes it a good candidate to be lazy loaded each time we read an Employee from this table. 
&lt;h3&gt;The Object Model&lt;/h3&gt;As the following object model shows, I created two entities: Employee and EmployeePhoto. I also created a unidirectional association between these two by defining a navigation property on the Employee class called EmployeePhoto: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;EmployeeID&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;LastName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;FirstName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Title&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;TitleOfCourtesy&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;?&amp;nbsp;BirthDate&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;?&amp;nbsp;HireDate&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Region&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;PostalCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Country&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;HomePhone&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Extension&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Notes&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;?&amp;nbsp;ReportsTo&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EmployeePhoto&lt;/span&gt;&amp;nbsp;EmployeePhoto&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EmployeePhoto&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color: rgb(43, 145, 175);"&gt;Key&lt;/span&gt;]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;EmployeeID&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;byte&lt;/span&gt;[]&amp;nbsp;Photo&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;PhotoPath&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;NorthwindContext&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt;&amp;nbsp;Employees&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;EmployeePhoto&lt;/span&gt;&amp;gt;&amp;nbsp;EmployeePhoto&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;} &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;How to Create a Table Splitting with Fluent API?&lt;/h3&gt;As also mentioned in the previous post, by convention, Code First always takes a unidirectional association as one-to-many unless we specify otherwise with fluent API. However, the fluent API codes that we have seen so far in this series won't let us create a table splitting. If we mark EmployeePhoto class as a complex type, we wouldn't be able to lazy load it anymore or if we create a shared primary key association then it will look for a separate table for the EmployeePhoto entity which we don't have in the Northwind database. The trick is to create a shared primary key association between Employee and EmployeePhoto entities but then instruct Code First to map them both to the same table. The following code shows how: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(e&amp;nbsp;=&amp;gt;&amp;nbsp;e.EmployeePhoto)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithRequiredPrincipal();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;gt;().ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"Employees"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;EmployeePhoto&lt;/span&gt;&amp;gt;().ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"Employees"&lt;/span&gt;);
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Note how we made both ends of the association required by using &lt;a href="http://msdn.microsoft.com/en-us/library/gg671317(v=VS.103).aspx" target="_blank"&gt;HasRequired&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/gg679449(v=VS.103).aspx" target="_blank"&gt;WithRequiredPrincipal&lt;/a&gt; methods, even though both the Photo and PhotoPath columns has been defined to allow NULLs. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;See the Lazy Loading of the Dependent Entity in Action&lt;/h3&gt;Now it's time to write a test to make sure that EF does not select the Photo column each time we query for an employee: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;NorthwindContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Employee&lt;/span&gt;&amp;nbsp;employee&amp;nbsp;=&amp;nbsp;context.Employees.First();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;byte&lt;/span&gt;[]&amp;nbsp;photo&amp;nbsp;=&amp;nbsp;employee.EmployeePhoto.Photo;
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;The following screen shot from the SQL Profiler shows the query that has been submitted to SQL Server as the result of reading the first employee object: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 20px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/11/EmployeeQuery.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 30px;"&gt;Accessing the EmployeePhoto navigation property of the employee object on the next line causes EF to submit a second query to the SQL Server to lazy (implicit) load the EmployeePhoto (By default, EF fetches associated objects and collections lazily whenever you access them): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 20px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/11/EmployeePhotoQuery.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 15px;"&gt;
&lt;h3&gt;Where to Use this Mapping?&lt;/h3&gt;I recommend using Table Splitting only for mapping of the legacy databases, actually that's the reason we start this post from an existing database like Northwind. For &lt;a href="http://en.wikipedia.org/wiki/Greenfield_project" target="_blank"&gt;green-field development&lt;/a&gt; scenarios consider using &lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;shared primary key association&lt;/a&gt; instead. There are several reasons why you may want to split the Employee table to two tables when designing a new physical data model for your application. In fact, it is very common for most applications to require a core collection of data attributes of any given entity, and then a specific subset of the noncore data attributes. For example, the core columns of the Employee table would include the columns required to store their name, address, and phone numbers; whereas noncore columns would include the Photo column. Because Employee.Photo is large, and required only by a few applications, you would want to consider splitting it off into its own table. This would help to improve retrieval access times for applications that select all columns from the Employee table yet do not require the photo. This also works pretty well for EF since it doesn't support lazy loading at the scalar property or complex type level. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Summary&lt;/h3&gt;In this post we learned about mapping a one-to-one association with table splitting. It enabled us to have lazy loading for the EmployeePhoto entity, something that we would have missed, had we mapped it with a complex type. We saw that on the database side it looks like a complex type mapping but on the object model it is &lt;u&gt;not&lt;/u&gt; a complex type since we mapped EmployeePhoto as an Entity with an object identifier (EmployeeID). In fact, it's a special kind of a shared primary key association where both the principal and dependent entities are mapped to one single table. This somehow exotic one-to-one association mapping should be reserved only for the mapping of existing legacy databases. &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;!--
&lt;tr&gt;
                
&lt;td&gt;
                    &lt;h3&gt;
                        References&lt;/h3&gt;
                    
&lt;ul&gt;
                        
&lt;li style="padding: 0px;"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank"
                            mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

                        
&lt;li style="padding: 0px;"&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank"
                            mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/a&gt;
                        &lt;/li&gt;

                    &lt;/ul&gt;

                &lt;/td&gt;

            &lt;/tr&gt;

--&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;
        a
        {
            color: #5A99FF;
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;
            line-height: 22px;
            padding-top: 10px; /*margin: 33px 0 11px;*/
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 5px;
        }
        
        .subTitle
        {
            font-weight: bold;
            padding-top: 15px;
            padding-bottom: 5px;
        }
        
        td
        {
            line-height: 18px;
        }
        li
        {
            padding-top: 5px;
            padding-bottom: 5px;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .myComment
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
    &lt;/style&gt;

&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7769737" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="Entity Framework 4.1" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework+4.1/default.aspx" /></entry><entry><title>Associations in EF Code First: Part 3 – Shared Primary Key Associations</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx</id><published>2011-04-14T06:12:00Z</published><updated>2011-04-14T06:12:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;This is the third post in a series that explains entity association mappings with EF Code First. This series includes: 
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx"&gt;Part 1 – Introduction and Basic Concepts&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;Part 2 – Complex Types&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Part 3 – Shared Primary Key Associations&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" target="_blank"&gt;Part 4 – Table Splitting&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" target="_blank"&gt;Part 5 – One-to-One Foreign Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx" target="_blank"&gt;Part 6 – Many-valued Associations&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;!-- ************************************************************************** --&gt;
&lt;tr&gt;
&lt;td&gt;In the previous &lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;blog post&lt;/a&gt; I demonstrated how to map a special kind of one-to-one association—a composition with complex types. We argued that the relationship between User and Address is best represented with a complex type mapping and we saw that this is usually the simplest way to represent one-to-one relationships but comes with some limitations. &lt;br&gt;&lt;br&gt;In today’s blog post I’m going to discuss how we can address those limitations by changing our mapping strategy. This is particularly useful for scenarios that we want a dedicated table for Address, so that we can map both User and Address as entities. One benefit of this model is the possibility for &lt;i&gt;shared references&lt;/i&gt;— another entity class (let’s say Shipment) can also have a reference to a particular Address instance. If a User has a reference to this instance, as her BillingAddress, the Address instance has to support shared references and needs its own identity. In this case, User and Address classes have a true &lt;i&gt;one-to-one association&lt;/i&gt;. 
&lt;h3&gt;Introducing the Revised Model&lt;/h3&gt;In this revised version, each User could have one BillingAddress (Billing Association). Also a Shipment always needs a destination address for delivery (Delivery Association). The following shows the class diagram for this domain model (note the multiplicities on association lines): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 15px; padding-bottom: 15px;" align="center"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/10/uml_class_drg.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;In this model we assumed that the billing address of the user is the same as her delivery address. Now let’s create the association mappings for this domain model. There are several choices, the first being a &lt;i&gt;One-to-One Primary Key Association&lt;/i&gt;. 
&lt;h3&gt;&lt;a title="SharedPrimaryKeyAssociations" name="SharedPrimaryKeyAssociations"&gt;&lt;/a&gt;Shared Primary Key Associations&lt;/h3&gt;Also know as &lt;i&gt;One-to-One Primary Key Associations&lt;/i&gt;, means &lt;u&gt;two related tables share the same primary key values&lt;/u&gt;. The &lt;u&gt;primary key of one table is also a foreign key of the other&lt;/u&gt;. Let’s see how we can create a primary key association mapping with Code First. 
&lt;h3&gt;How to Implement a One-to-One Primary Key Association with Code First&lt;/h3&gt;First, we start with the POCO classes. As you can see, we've defined &lt;i&gt;BillingAddress&lt;/i&gt; as a navigation property on User class and another one on Shipment class named &lt;i&gt;DeliveryAddress&lt;/i&gt;. Both associations are unidirectional since we didn't define related navigation properties on Address class as for User and Shipment. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;BillingAddress&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;AddressId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Shipment&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;ShipmentId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;State&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;DeliveryAddress&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;&amp;nbsp;Users&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;gt;&amp;nbsp;Addresses&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Shipment&lt;/span&gt;&amp;gt;&amp;nbsp;Shipments&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;How Code First Sees the Associations in our Object Model: One-to-Many&lt;/h3&gt;Code First reads the model and tries to figure out the multiplicity of the associations. Since the associations are unidirectional, Code First takes this as if one Address has many Users and Many Shipments and will create a &lt;i&gt;one-to-many&lt;/i&gt; association for each of them. In other words, &lt;u&gt;a unidirectional association is always inferred as One-to-Many&lt;/u&gt; by Code First. So, what we were hoping for —a one-to-one association, is not inline with the Code First conventions. 
&lt;h3&gt;How to Change the Multiplicity of the Associations to One-to-One by Using the Conventions&lt;/h3&gt;Obviously, one way to turn our associations to one-to-one is by making them bidirectional. That is, adding a new navigation property to Address class of type User and another one of type Shipment. By doing that we simply signal Code First that we are looking to have one-to-one associations since for example User has an Address and also Address has a User. Therefore, Code First will change the multiplicity to one-to-one and this will solve the problem. 
&lt;h3&gt;Should We Make the Associations Bidirectional? &lt;/h3&gt;As always, the decision is up to us and depends on whether we need to navigate through our objects in that direction in the application code. In this case, we’d probably conclude that the bidirectional association doesn’t make much sense. If we call anAddress.User, we are saying “give me the user who has this address”, not a very reasonable request. So this is not a good option. Instead we'll keep our object model as it is and will explicitly ask Code First to make our associations one-to-one. 
&lt;h3&gt;How to Change the Multiplicity to One-to-One with Fluent API&lt;/h3&gt;The following code is all that is needed to make the associations to be one-to-one. Note how the multiplicities in the UML class diagram (e.g. 1 on User and 0..1 on address) has been translated to the fluent API code by using &lt;a href="http://msdn.microsoft.com/en-us/library/gg671317(v=VS.103).aspx" target="_blank"&gt;HasRequired&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/gg671230(v=VS.103).aspx"&gt;HasOptional&lt;/a&gt; methods: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithRequired();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Shipment&lt;/span&gt;&amp;gt;().HasRequired(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithOptional();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Also it worth noting that when we are mapping a one-to-one association with fluent API, we don't need to specify the foreign key as we would do when mapping a one-to-many association with &lt;a href="http://msdn.microsoft.com/en-us/library/gg671294(v=VS.103).aspx" target="_blank"&gt;HasForeignKey&lt;/a&gt; method. Since EF only supports one-to-one associations on primary keys, it will automatically create the relationship in the database on the primary keys.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;


&lt;td&gt;
&lt;h3&gt;Database Schema&lt;/h3&gt;The mapping result for our object model is as follows (note the Identity column on Users table): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px; padding-left: 10px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/10/shared_pk_schema.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 0px;"&gt;
&lt;h3&gt;Referential Integrity&lt;/h3&gt;In relational database design the referential integrity rule states that each non-null value of a foreign key must match the value of some primary key. But wait, how does it even applies here? All we have is just three primary keys referencing each other! Who is the primary key and who is the foreign key? The best way to find the answer of this question is to take a look at the properties of the relationships in the database that has been created by Code First: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 30px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/10/user_address_detail.png"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 30px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/10/shipment_address_detail.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 30px;"&gt;As you can see, Code First adds a foreign key constraint which links the primary key of the Addresses table to the primary key of the Users table and adds another foreign key constraint that links the primary key of the Shipments table to the primary key of the Addresses table. The foreign key constraint means that a user has to exist for a particular address but &lt;u&gt;not&lt;/u&gt; the other way around. In other words, the database guarantees that an Addresses row’s primary key references a valid Users primary key and a Shipments row’s primary key references a valid Addresses primary key. 
&lt;h3&gt;How Code First Determines the Principal and Dependent Ends in an Association?&lt;/h3&gt;Code First has rules to determine the principal and dependent ends of an association. For &lt;u&gt;one-to-many relationships the many end is always the dependent&lt;/u&gt;, but it gets a little tricky in one-to-one associations. In one-to-one associations Code First decides based on our object model, and possible data annotations or fluent API code that we may have. For example in this case, we used the following fluent API code to configure the User-Address association: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress).WithRequired();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;This reads as "User entity has an optional association with one Address object but this association is required for Address entity". For Code First this is good enough to make the decision: It marked User as the principal end and Address as the dependent end in the association. Since we have the same fluent API code for the second association between Address and Shipment, it marks Address as the principal end and Shipment as the dependent end in this association as well. &lt;br&gt;&lt;br&gt;This decision has some consequences. In fact, the &lt;u&gt;referential integrity that we saw, is the first result of this Code First's principal/dependent decision&lt;/u&gt;. 
&lt;h3&gt;Second Result of Code First's Principal/Dependent Decision: Database Identity &lt;/h3&gt;If you take a closer look at the above DB schema, you'll notice that only UserId has a regular identifier generator (aka &lt;i&gt;Identity&lt;/i&gt; or &lt;i&gt;Sequence&lt;/i&gt;) and AddressId and ShipmentId does not. This is a very important consequence of the principal/dependent decision for one-to-one associations: &lt;u&gt;the dependent primary key will become non-Identity by default&lt;/u&gt;. This make sense because they share their primary key values and only one of them can be auto generated and we need to take care of providing valid keys for the rest. &lt;/td&gt;
&lt;/tr&gt;
&lt;!--
&lt;tr&gt;
&lt;td&gt;
&lt;DIV class=title&gt;Third Result of Code First's Principal/Dependent Decision: Transitive Deletion&lt;/DIV&gt;
Imagine that we want to delete a User object which has an Address. We have to pass this object to
the &lt;span class="code"&gt;Remove()&lt;/span&gt; method on the Users DbSet; 
it’s now in &lt;span class="code"&gt;Deleted&lt;/span&gt; state and will be gone from the database when the &lt;span class="code"&gt;SaveChanges()&lt;/span&gt;is invoked on persistence 
context (we assumed that we have a User with UserId=1 in the database):
&lt;/td&gt;

&lt;/tr&gt;


&lt;tr&gt;
    
&lt;td&gt;
    
        
&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span 
            style="color: #2b91af"&gt;EntityMappingContext&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span 
            style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;EntityMappingContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af"&gt;User&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;context.Users.Find(1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Remove(user);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;
    
    &lt;/td&gt;

&lt;/tr&gt;

--&gt;&lt;!--
&lt;tr&gt;
&lt;td&gt;
When we run this code the User and her related address will be gone. But wait, how that is even possible?
If we try to manually delete this User from the database, we’ll get a foreign key constraint violation if an Address holds a reference
to the User. It’s our responsibility to delete the related Address before trying to delete the User.
However, our code ran perfectly fine and deleted the User and her Address. Looking into SQL Profiler will reveal the mystery:
&lt;/td&gt;

&lt;/tr&gt;

--&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;What about Cascade Deletes?&lt;/h3&gt;As we saw, each Address always belongs to one User and each Shipment always delivered to one single Address. We want to make sure that when we delete a User the possible dependent rows on Address and Shipment also get deleted in the database. In fact, this is one of the &lt;a href="http://www.agiledata.org/essays/databaseRefactoringCatalogReferentialIntegrity.html" target="_blank"&gt;Referential Integrity Refactorings&lt;/a&gt; which called &lt;a href="http://databaserefactoring.com/IntroduceCascadingDelete.html" target="_blank"&gt;Introduce Cascading Delete&lt;/a&gt;. The primary reason we would apply "Introduce Cascading Delete" is to preserve the referential integrity of our data by ensuring that related rows are appropriately deleted when a parent row is deleted. By default, Code First does &lt;u&gt;not&lt;/u&gt; enable cascade delete when it creates a one-to-one relationship in the database. As always we can override this convention by fluent API: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithRequired()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WillCascadeOnDelete();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Shipment&lt;/span&gt;&amp;gt;().HasRequired(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithOptional()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WillCascadeOnDelete();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;What the Additional Methods Like WithRequiredDependent are for?&lt;/h3&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/gg671317(v=VS.103).aspx" target="_blank"&gt;HasRequired&lt;/a&gt; method returns an object of type &lt;a href="http://msdn.microsoft.com/en-us/library/gg679232(v=VS.103).aspx" target="_blank"&gt;RequiredNavigationPropertyConfiguration&lt;/a&gt; which defines two special methods called &lt;a href="http://msdn.microsoft.com/en-us/library/gg671273(v=VS.103).aspx" target="_blank"&gt;WithRequiredDependent&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/gg671256(v=VS.103).aspx" target="_blank"&gt;WithRequiredPrincipal&lt;/a&gt; in addition to the typical &lt;em&gt;WithMany&lt;/em&gt; and &lt;em&gt;WithOptional&lt;/em&gt; methods that we usually use. We saw that the only reason Code First could figure out principal and dependent in our associations was because our fluent API code clearly specified one end as Required and the other as Optional. But what if both endpoints are required or both are optional in the association? For example consider a scenario that a User always has one Address and Address always has one User (required on both end). Now Code First cannot pick up the principal and dependent ends on its own and that's exactly where methods like &lt;i&gt;WithRequiredDependent&lt;/i&gt; come into play. In other words, this scenario ultimately need to be configured by fluent API and fluent API is designed in a way that will force you to explicitly specify who is dependent and who is principal in a required-required or optional-optional association scenario. &lt;br&gt;&lt;br&gt;For example, this fluent API code shows how we can configure the User-Address association where both ends are required: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;().HasRequired(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress).WithRequiredDependent();
&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Taking a closer look at the &lt;a href="http://msdn.microsoft.com/en-us/library/gg679232(v=VS.103).aspx" target="_blank"&gt;RequiredNavigationPropertyConfiguration&lt;/a&gt; type also shows the idea:&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;RequiredNavigationPropertyConfiguration&lt;/span&gt;&amp;lt;TEntityType,&amp;nbsp;TTargetEntityType&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DependentNavigationPropertyConfiguration&lt;/span&gt;&amp;lt;TEntityType,&amp;nbsp;TTargetEntityType&amp;gt;&amp;nbsp;WithMany();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CascadableNavigationPropertyConfiguration&lt;/span&gt;&amp;nbsp;WithOptional();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CascadableNavigationPropertyConfiguration&lt;/span&gt;&amp;nbsp;WithRequiredDependent();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CascadableNavigationPropertyConfiguration&lt;/span&gt;&amp;nbsp;WithRequiredPrincipal();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;As you can see, if you want to go another Required after &lt;span class="code"&gt;HasRequired&lt;/span&gt; method, you have to either call &lt;span class="code"&gt;WithRequiredDependent&lt;/span&gt; or &lt;span class="code"&gt;WithRequiredPrincipal&lt;/span&gt; since there is no WithRequired method defined on &lt;span class="code"&gt;RequiredNavigationPropertyConfiguration&lt;/span&gt; class.&lt;/td&gt;
&lt;/tr&gt;
&lt;!--
    Therefore, by design going another Optional after &lt;span class="code"&gt;HasOptional()&lt;/span&gt; method is not possible unless you explicitly clarify
    the principal/dependent situation by 
    either calling &lt;span class="code"&gt;WithOptionalDependent()&lt;/span&gt; or &lt;span class="code"&gt;WithOptionalPrincipal()&lt;/span&gt; since
    there is no &lt;span class="code"&gt;WithOptional()&lt;/span&gt; method on this &lt;span class="code"&gt;OptionalNavigationPropertyConfiguration&lt;/span&gt; class.
--&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Working with the Model&lt;/h3&gt;Here is an example for adding a new user along with its billing address. EF is smart enough to use the newly generated UserId for the AddressId as well: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;billingAddress&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Street&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"Main&amp;nbsp;St."&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"Seattle"&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Name&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"Morteza"&lt;/span&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BillingAddress&amp;nbsp;=&amp;nbsp;billingAddress
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Add(user);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;The following code is an example of adding a new Address and Shipment for an existing User (assuming that we have a User with UserId = 1 in the database): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;deliveryAddress&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AddressId&amp;nbsp;=&amp;nbsp;1,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Street&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"Main&amp;nbsp;St."&lt;/span&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Shipment&lt;/span&gt;&amp;nbsp;shipment&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Shipment&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShipmentId&amp;nbsp;=&amp;nbsp;1,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"Shipped"&lt;/span&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DeliveryAddress&amp;nbsp;=&amp;nbsp;deliveryAddress
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Shipments.Add(shipment);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;Limitations of This Mapping&lt;/h3&gt;There are two important limitations to associations mapped as shared primary key: 
&lt;ul&gt;
&lt;li style="padding-top: 0px;" class="subTitle"&gt;Difficulty in Saving Related Objects&lt;/li&gt;
The main difficulty with this approach is ensuring that associated instances are assigned the same primary key value when the objects are saved. For example, when adding a new Address object, it's our responsibility to provide a unique AddressId that is also valid (a User can be found with such a value as UserId.) 
&lt;li class="subTitle"&gt;Multiple Addresses for User is Not Possible&lt;/li&gt;
With this mapping we cannot have more than one Address for User. At the beginning of this post, when we introduce our model, we assumed that the user has the same address for billing and delivery. But what if that's not the case? What if we also want to add a Home address to User for the deliveries? In the current setup, each row in the User table has at most one corresponding row in the Address table. Two addresses would require an additional address table, and this mapping style therefore wouldn’t be adequate. &lt;/ul&gt;
&lt;h3&gt;Summary&lt;/h3&gt;In this post we learned about one-to-one associations which shared primary key is just one way to implement it. Shared primary key associations aren’t uncommon but are relatively rare. In many schemas, a one-to-one association is represented with a foreign key field and a unique constraint. In the next posts we will revisit the same domain model and will learn about other ways to map one-to-one associations that does not have the limitations of the shared primary key association mapping. &lt;br&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;!-- ************************************************************************** --&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li style="padding: 0px;"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

&lt;li style="padding: 0px;"&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank" mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;    
    a
    {            
        color: #5A99FF;
    }
    
    a:visited 
    {
        color: #5A99FF;
    }
    
    h3 
    {
        border-bottom: 1px solid #DDDDDD;
        color: #333333;
        font-size: 14px;
        font-family: Segoe UI,arial,serif;
        padding-bottom: 0.2em;
        font-weight: normal;            
        line-height: 22px;
        padding-top: 10px;        
        /*margin: 33px 0 11px;*/
    }
    
    .padTop5
    {
        padding-top: 5px;    
    }
     
    .padTop10
    {
        padding-top: 5px;    
    }
        
    .subTitle
    {
        
        
        font-weight: bold;        
        padding-top: 15px;
        padding-bottom: 5px;
    }
    
    td         
    {
            line-height: 18px;
    }
    li
    {
        padding-top:5px;
        padding-bottom: 5px;
    }
    
    .code, .typeName
    {
        font-family: consolas;           
    }
    
    .typeName
    {
        color: #2b91af;
    }
    
    .myComment
    {
        border-bottom:#cdcdcd 1px solid;
        border-left:#cdcdcd 1px solid;        
        line-height:18px;
        background-color:#f2f2f2;
        padding-top:9px;
        padding-left:9px;
        padding-bottom:9px;
        padding-right:9px;
        border-top:#cdcdcd 1px solid;
        border-right:#cdcdcd 1px solid;        
    }
    
&lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7757048" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="Entity Framework 4.1" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework+4.1/default.aspx" /></entry><entry><title>Associations in EF Code First: Part 2 – Complex Types</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx</id><published>2011-03-28T20:13:00Z</published><updated>2011-03-28T20:13:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;This is the second post in a series that explains entity association mappings with EF Code First. This series includes: 
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx"&gt;Part 1 – Introduction and Basic Concepts&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Part 2 – Complex Types&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;Part 3 – Shared Primary Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" target="_blank"&gt;Part 4 – Table Splitting&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" target="_blank"&gt;Part 5 – One-to-One Foreign Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx" target="_blank"&gt;Part 6 – Many-valued Associations&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;
&lt;h3&gt;Introducing the Model&lt;/h3&gt;First, let's review the model that we are going to use in order to create a Complex Type with EF Code First. It's a simple object model which consists of two classes: &lt;em&gt;User&lt;/em&gt; and &lt;em&gt;Address.&lt;/em&gt; Each user could have one billing address (or nothing at all–note the multiplicities on the class diagram). The Address information of a User is modeled as a separate class as you can see in the class diagram below: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px; padding-bottom: 10px;" align="center"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/09/classDrg.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/09/classDrg.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;In object-modeling terms, this association is a kind of &lt;em&gt;aggregation&lt;/em&gt;—a &lt;em&gt;part-of&lt;/em&gt; relationship. Aggregation is a strong form of association; it has some additional semantics with regard to the lifecycle of objects. In this case, we have an even stronger form, &lt;em&gt;composition&lt;/em&gt;, where the lifecycle of the part is fully dependent upon the lifecycle of the whole. 
&lt;h3&gt;Fine-grained Domain Models &lt;/h3&gt;The motivation behind this design was to achieve &lt;i&gt;Fine-grained&lt;/i&gt; domain models. In crude terms, fine-grained means &lt;u&gt;more classes than tables&lt;/u&gt;. For example, a user may have both a billing address and a home address. In the database, you may have a single Users table with the columns BillingStreet, BillingCity, and BillingZipCode along with HomeStreet, HomeCity, and HomeZipCode. There are good reasons to use this somewhat &lt;i&gt;denormalized&lt;/i&gt; relational model (performance, for one). In our object model, we can use the same approach, representing the two addresses as six string-valued properties of the User class. But it’s much better to model this using an Address class, where User has the BillingAddress and HomeAddress properties. This object model achieves &lt;u&gt;improved cohesion&lt;/u&gt; and &lt;u&gt;greater code reuse&lt;/u&gt; and is more understandable. 
&lt;h3&gt;Complex Types are Objects with No Identity&lt;/h3&gt;When it comes to the actual C# implementation, there is no difference between this composition and other weaker styles of association but in the context of ORM, there is a big difference: A composed class is often a candidate &lt;em&gt;Complex Type&lt;/em&gt; (aka &lt;a href="http://martinfowler.com/eaaCatalog/valueObject.html" target="_blank" mce_href="http://martinfowler.com/eaaCatalog/valueObject.html"&gt;Value Object&lt;/a&gt;). But C# has no concept of composition—a class or property can’t be marked as a composition. The only difference is the object identifier: &lt;u&gt;a complex type has no individual identity&lt;/u&gt; (e.g. there is no AddressId defined on Address class) which make sense because when it comes to the database everything is going to be saved into one single table.&lt;br&gt;
&lt;h3&gt;Complex Type Discovery&lt;/h3&gt;Code First has a concept of &lt;em&gt;Complex Type Discovery&lt;/em&gt; that works based on a set of &lt;a href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx" target="_blank" mce_href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx"&gt;Conventions&lt;/a&gt;. The convention is that if Code First discovers a class where &lt;span style="text-decoration: underline;"&gt;a primary key cannot be inferred&lt;/span&gt;, and &lt;span style="text-decoration: underline;"&gt;no primary key is registered through Data Annotations or the fluent API&lt;/span&gt;, then the type will be &lt;i&gt;automatically&lt;/i&gt; registered as a complex type. Complex type detection also requires that the type does not have properties that reference entity types (i.e. all the properties must be scalar types) and is not referenced from a collection property on another type. 
&lt;h3&gt;How to Implement a Complex Type with EF Code First&lt;/h3&gt;The following shows the implementation of the introduced model in Code First: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;&amp;nbsp;Users&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;With code first, this is &lt;span style="text-decoration: underline;"&gt;all of the code&lt;/span&gt; we need to write to create a complex type, we do not need to configure any additional database schema mapping information through Data Annotations or the fluent API. 
&lt;h3&gt;Complex Types: Splitting a Table Across Multiple Types&lt;/h3&gt;The mapping result for this object model is as follows (Note how Code First prefixes the complex type's column names with the name of the complex type): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/09/UsersTable.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/09/UsersTable.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Complex Types are Required&lt;/h3&gt;As a limitation of EF in general, complex types are always considered required. To see this limitation in action, let's try to add a record to the Users table: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Name&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"Morteza"&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Add(user);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Surprisingly, this code throws a &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.updateexception.aspx" target="_blank;" mce_href="http://msdn.microsoft.com/en-us/library/system.data.updateexception.aspx"&gt;System.Data.UpdateException&lt;/a&gt; at runtime with this message: &lt;br&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 8px;"&gt;
&lt;div style="width: 328px; padding-top: 5px; padding-bottom: 5px; padding-left: 5px; background-color: rgb(240, 240, 240);"&gt;&lt;i&gt;Null value for non-nullable member. Member: 'Address'.&lt;/i&gt; &lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;If we initialize the address object, the exception would go away and the user will be successfully saved into the database: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/09/CodeSnippet.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/09/CodeSnippet.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Now if we read back the inserted record from the database, EF will return an Address object with Null values on all of its properties (Street, City and ZipCode). This means that even when you store a complex type object with all null property values, EF still returns an initialized complex type when the owning entity (e.g. User) is retrieved from the database.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Explicitly Register a Type as Complex&lt;/h3&gt;You saw that in our model, we did not use any data annotation or fluent API code to designate the Address as a complex type, yet Code First detects it as a complex type based on Complex Type Discovery. But what if our domain model requires a new property like "Id" on Address class? This new Id property is just another scalar non-primary key property that represents let's say another piece of information about Address. Now Code First &lt;i&gt;can&lt;/i&gt; (and will) infer a key and therefore marks Address as an entity that has its own mapping table unless we specify otherwise. This is where explicit complex type registration comes into play. There are two ways to register a type as complex: 
&lt;li class="subTitle"&gt;Using Data Annotations&lt;/li&gt;
EF 4.1 introduces a new attribute in &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=VS.103).aspx"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt; namespace called &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.complextypeattribute(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.complextypeattribute(v=VS.103).aspx"&gt;ComplexTypeAttribute&lt;/a&gt;. All we need to do is to place this attribute on our Address class: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;ComplexType&lt;/span&gt;]
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Id&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;This will keep Address as a complex type in our model despite its Id property.&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;
&lt;li class="subTitle"&gt;Using Fluent API&lt;/li&gt;
Alternatively, we can use &lt;a href="http://msdn.microsoft.com/en-us/library/gg679474(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/gg679474(v=VS.103).aspx"&gt;ComplexType&lt;/a&gt; generic method defined on &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbmodelbuilder(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbmodelbuilder(v=VS.103).aspx"&gt;DbModelBuilder&lt;/a&gt; class to register our Address type as complex:&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.ComplexType&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;gt;();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Best Practices When Working with Complex Types &lt;/h3&gt;
&lt;ul&gt;
&lt;li style="padding-top: 5px;" class="subTitle"&gt;Always Initialize the Complex Type:&lt;/li&gt;
Because of the problem we saw, I recommended always initialize the complex type inside its owning entity's constructor. 
&lt;li class="subTitle"&gt;Add a Read Only Property to the Complex Type for Null Value Checking:&lt;/li&gt;
Defining a non-persistent read only property like &lt;i&gt;HasValue&lt;/i&gt; will help to test for null values. 
&lt;li class="subTitle"&gt;Consider Always Explicitly Registering a ComplexType:&lt;/li&gt;
Even if your class is automatically detected as a complex type by Code First, I still recommend to mark it with ComplexTypeAttribute. Not only that helps your object model to be more readable but also ensures that your complex types will stay as complex as your model evolves in your project. Of course if you have a &lt;a href="http://martinfowler.com/eaaCatalog/domainModel.html" target="_blank" mce_href="http://martinfowler.com/eaaCatalog/domainModel.html"&gt;domain layer&lt;/a&gt; then you should use the fluent API's ComplexType method instead since coupling your POCO domain model to the EntityFramework assembly (where ComplexTypeAttribute lives) is the last thing you want to do in your layered architecture! &lt;/ul&gt;
Therefore, our final object model will be: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;User()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Address&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
[&lt;span style="color: rgb(43, 145, 175);"&gt;ComplexType&lt;/span&gt;]
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&amp;nbsp;HasValue
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&amp;nbsp;(Street&amp;nbsp;!=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&amp;nbsp;||&amp;nbsp;ZipCode&amp;nbsp;!=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&amp;nbsp;||&amp;nbsp;City&amp;nbsp;!=&amp;nbsp;&lt;span style="color: blue;"&gt;null&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;The interesting point is that we did not have to explicitly exclude the HasValue property from the mapping above. Since HasValue has been defined as a read only property (i.e. there is no setter), EF Code First will be ignoring it based on conventions, which makes sense since a read only property is most probably representing a computed value and does not need to be persisted in the database. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Customize Complex Type's Property Mappings at Entity Level&lt;/h3&gt;We can customize the individual property mappings of the complex type. For example, The Users table now contains, among others, the columns Address_Street, Address_PostalCode, and Address_City. We can rename these with &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.columnattribute(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.columnattribute(v=VS.103).aspx"&gt;ColumnAttribute&lt;/a&gt;: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"Street"&lt;/span&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;PostalCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Fluent API can give us the same result as well: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.ComplexType&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Property(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.Street)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasColumnName(&lt;span style="color: rgb(163, 21, 21);"&gt;"Street"&lt;/span&gt;);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Any other entity table that contains complex type fields (say, a Customer class that also has an Address) uses the same column options. Sometimes we’ll want to override the settings we made inside the complex type from outside for a particular entity. This is often the case when we try to derive an object model from a legacy database. For example, here is how we can rename the Address columns for Customer class: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Name&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;CustomerId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;PhoneNumber&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
[&lt;span style="color: rgb(43, 145, 175);"&gt;ComplexType&lt;/span&gt;]
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color: rgb(43, 145, 175);"&gt;Column&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"Street"&lt;/span&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ZipCode&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;gt;&amp;nbsp;Users&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;&amp;gt;&amp;nbsp;Customers&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Customer&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Property(c&amp;nbsp;=&amp;gt;&amp;nbsp;c.Address.Street)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasColumnName(&lt;span style="color: rgb(163, 21, 21);"&gt;"Customer_Street"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Complex Types and the New Change Tracking API&lt;/h3&gt;As part of the new &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=VS.103).aspx"&gt;DbContext&lt;/a&gt; API, EF 4.1 came with a new set of change tracking API that enables us to access Original and Current values of our entities. The &lt;i&gt;Original Values&lt;/i&gt; are the values the entity had when it was queried from the database. The &lt;i&gt;Current Values&lt;/i&gt; are the values the entity has now. This feature also fully supports complex types. &lt;br&gt;&lt;br&gt;The entry point for accessing the new change tracking API is DbContext's &lt;a href="http://msdn.microsoft.com/en-us/library/gg696578(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/gg696578(v=VS.103).aspx"&gt;Entry&lt;/a&gt; method which returns an object of type &lt;a href="http://msdn.microsoft.com/en-us/library/gg696410(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/gg696410(v=VS.103).aspx"&gt;DbEntityEntry&lt;/a&gt;. DbEntityEntry contains a &lt;a href="http://msdn.microsoft.com/en-us/library/gg671208(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/gg671208(v=VS.103).aspx"&gt;ComplexProperty&lt;/a&gt; method that returns a &lt;a href="http://msdn.microsoft.com/en-us/library/gg679360(v=VS.103).aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/gg679360(v=VS.103).aspx"&gt;DbComplexPropertyEntry&lt;/a&gt; object where we can access the original and current values: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Context&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;context.Users.Find(1);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;originalValues&amp;nbsp;=&amp;nbsp;context.Entry(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ComplexProperty(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.OriginalValue;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Address&lt;/span&gt;&amp;nbsp;currentValues&amp;nbsp;=&amp;nbsp;context.Entry(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ComplexProperty(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.CurrentValue;
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;Also we can drill down into the complex object and read or set properties of it using chained calls: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;city&amp;nbsp;=&amp;nbsp;context.Entry(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ComplexProperty(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Property(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.City)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.CurrentValue;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;We can even get the nested properties using a single lambda expression: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;city&amp;nbsp;=&amp;nbsp;context.Entry(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Property(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address.City)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.CurrentValue;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Limitations of This Mapping&lt;a title="Limitations" name="Limitations"&gt;&lt;/a&gt;&amp;nbsp;&lt;/h3&gt;There are three important limitations to classes mapped as Complex Types: 
&lt;li class="subTitle"&gt;Shared References is Not Possible:&lt;/li&gt;
The Address Complex Type doesn’t have its own database identity (primary key) and so can’t be referred to by any object other than the containing instance of User (e.g. a Shipping class that also needs to reference the same User Address, cannot do so). 
&lt;li class="subTitle"&gt;No Elegant Way to Represent a Null Reference:&lt;/li&gt;
As we saw there is no elegant way to represent a null reference to an Address. When reading from database, EF Code First always initialize Address object even if values in all mapped columns of the complex type are null. 
&lt;li class="subTitle"&gt;Lazy Loading of Complex Types is Not Possible:&lt;/li&gt;
Note that EF always initializes the property values of a complex type right away, when the entity instance that holds the complex object is loaded. EF does not support lazy loading for complex types (same limitation also exists if you want to have lazy loading for scalar properties of an entity). This is inconvenient when we have to deal with potentially large values (for example, a property of type &lt;span class="code"&gt;byte[]&lt;/span&gt; on the Address complex type which has been mapped to a VARBINARY column on Users table and holds an image of the location described by the Address.). 
&lt;h3&gt;Summary&lt;/h3&gt;In this post we learned about fine-grained domain models which complex type is just one example of it. Fine-grained is fully supported by EF Code First and is known as the most important requirement for a rich domain model. Complex type is usually the simplest way to represent one-to-one relationships and because the lifecycle is almost always dependent in such a case, it’s either an aggregation or a composition in UML. In the next posts we will revisit the same domain model and will learn about other ways to map a one-to-one association that does not have the limitations of the complex types. 
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li style="padding: 0px;"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

&lt;li style="padding: 0px;"&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank" mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;    
    a
    {            
        color: #5A99FF;
    }
    
    a:visited 
    {
        color: #5A99FF;
    }
    
    h3 
    {
        border-bottom: 1px solid #DDDDDD;
        color: #333333;
        font-size: 14px;
        font-family: Segoe UI,arial,serif;
        padding-bottom: 0.2em;
        font-weight: normal;            
        line-height: 22px;
        padding-top: 10px;        
        /*margin: 33px 0 11px;*/
    }
    
    .padTop5
    {
        padding-top: 5px;    
    }
     
    .padTop10
    {
        padding-top: 5px;    
    }
        
    .subTitle
    {
        
        
        font-weight: bold;        
        padding-top: 15px;
        padding-bottom: 5px;
    }
    
    td         
    {
            line-height: 18px;
    }
    li
    {
        padding-top:10px;
    }
    
    .code, .typeName
    {
        font-family: consolas;           
    }
    
    .typeName
    {
        color: #2b91af;
    }
    
    .myComment
    {
        border-bottom:#cdcdcd 1px solid;
        border-left:#cdcdcd 1px solid;
        padding-bottom:9px;
        line-height:18px;
        background-color:#f2f2f2;
        padding-left:9px;
        padding-right:9px;
        border-top:#cdcdcd 1px solid;
        border-right:#cdcdcd 1px solid;
        padding-top:9px;
    }
    
&lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7736608" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="Entity Framework 4.1" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework+4.1/default.aspx" /></entry><entry><title>Associations in EF Code First: Part 1 – Introduction and Basic Concepts</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx</id><published>2011-03-27T05:04:00Z</published><updated>2011-03-27T05:04:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Earlier this month the data team shipped the &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-release-candidate-available.aspx" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-release-candidate-available.aspx"&gt;Release Candidate&lt;/a&gt; of EF 4.1. The most exciting feature of EF 4.1 is &lt;i&gt;Code First&lt;/i&gt;, a new development pattern for EF which provides a really elegant and powerful code-centric way to work with data as well as an alternative to the existing &lt;i&gt;Database First&lt;/i&gt; and &lt;i&gt;Model First&lt;/i&gt; patterns. Code First is designed based on &lt;a href="http://en.wikipedia.org/wiki/Convention_over_configuration" target="_blank" mce_href="http://en.wikipedia.org/wiki/Convention_over_configuration"&gt;Convention over Configuration&lt;/a&gt; paradigm and focused around defining your model using C#/VB.NET classes, these classes can then be mapped to an existing database or be used to generate a database schema. Additional configuration can be supplied using Data Annotations or via a fluent API. &lt;br&gt;&lt;br&gt;I’m a big fan of the EF Code First approach, and wrote several blog posts about it based on its CTP5 build: 
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx"&gt;Associations in EF Code First CTP5: Part 1 – Complex Types&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx"&gt;Associations in EF Code First CTP5: Part 2 – Shared Primary Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx"&gt;Associations in EF Code First CTP5: Part 3 – One-to-One Foreign Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx"&gt;Inheritance with EF Code First CTP5: Part 1 – Table per Hierarchy (TPH) &lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx"&gt;Inheritance with EF Code First CTP5: Part 2 – Table per Type (TPT)&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx"&gt;Inheritance with EF Code First CTP5: Part 3 – Table per Concrete Type (TPC)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
Compare to CTP5, EF 4.1 release is more about bug fixing and bringing it to a go-live quality level than anything else. Pretty much all of the API that has been introduced in CTP5 is still exactly the same (except very few changes including renaming of DbDatabase and ModelBuilder classes as well as consolidation of IsIndependent fluent API method). Therefore, the above blog posts are still usable and can (hopefully) help you in your Code First development. Having said that, I decided to complete my Code First articles by starting a whole new series instead of doing post maintenance on the current CTP5 ones. 
&lt;h3&gt;A Note For Those Who are New to EF and Code-First&lt;/h3&gt;If you choose to learn EF you've chosen well. If you choose to learn EF with Code First you've done even better. To get started, you can find an EF 4.1 Code First walkthrough by ADO.NET team &lt;a href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx"&gt;here&lt;/a&gt;. In this series, I assume you already setup your machine to do Code First development and also that you are familiar with Code First fundamentals and basic concepts. 
&lt;h3&gt;Code First And Associations&lt;/h3&gt;I will start my EF 4.1 Code First articles by a series on entity association mappings. You will see that when it comes to associations, Code First brings ultimate power and flexibility. This series will come in several parts including: 
&lt;ul&gt;
&lt;li&gt;Part 1 – Introduction and Basic Concepts&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx" target="_blank"&gt;Part 2 – Complex Types&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" target="_blank"&gt;Part 3 – Shared Primary Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" target="_blank"&gt;Part 4 – Table Splitting&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx" target="_blank"&gt;Part 5 – One-to-One Foreign Key Associations&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx" target="_blank"&gt;Part 6 – Many-valued Associations&lt;/a&gt;&lt;/li&gt;

&lt;/ul&gt;
&lt;h3&gt;Why Starting with Association Mappings? &lt;/h3&gt;From my experience with the EF user community, I know that the first thing many developers try to do when they begin using EF (specially when having a Code First approach) is a mapping of a parent/children relationship. This is usually the first time you encounter collections. It’s also the first time you have to think about the differences between entities and value types, or the type of relationships between your entities. Managing the associations between classes and the relationships between tables is at the heart of ORM. Most of the difficult problems involved in implementing an ORM solution relate to association management. &lt;br&gt;&lt;br&gt;In order to build a solid foundation for our discussion, we will start by learning about some of the core concepts around the relationship mapping and will leave the discussion for each type of entity associations to the next posts in this series. 
&lt;h3&gt;What is Mapping?&lt;/h3&gt;&lt;i&gt;Mapping&lt;/i&gt; is the act of determining how objects and their relationships are persisted in permanent data storage, in our case, relational databases. 
&lt;h3&gt;What is Relationship Mapping?&lt;/h3&gt;A mapping that describes how to persist a relationship (association, aggregation, or composition) between two or more objects. 
&lt;h3&gt;Types of Relationships&lt;/h3&gt;There are two categories of object relationships that we need to be concerned with when mapping associations. The first category is based on &lt;i&gt;multiplicity&lt;/i&gt; and it includes three types: 
&lt;ul&gt;
&lt;li style="padding-top: 0px;"&gt;&lt;b&gt;One-to-one relationships:&lt;/b&gt; This is a relationship where the maximums of each of its multiplicities is one. &lt;/li&gt;

&lt;li&gt;&lt;b&gt;One-to-many relationships:&lt;/b&gt; Also known as a many-to-one relationship, this occurs when the maximum of one multiplicity is one and the other is greater than one. &lt;/li&gt;

&lt;li style="padding-bottom: 5px;"&gt;&lt;b&gt;Many-to-many relationships:&lt;/b&gt; This is a relationship where the maximum of both multiplicities is greater than one. &lt;/li&gt;
&lt;/ul&gt;
The second category is based on &lt;em&gt;directionality&lt;/em&gt; and it contains two types: 
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Uni-directional relationships: &lt;/b&gt;when an object knows about the object(s) it is related to but the other object(s) do not know of the original object. To put this in EF terminology, when a navigation property exists only on one of the association ends and not on the both. &lt;/li&gt;

&lt;li&gt;&lt;b&gt;Bi-directional relationships:&lt;/b&gt; When the objects on both end of the relationship know of each other (i.e. a navigation property defined on both ends).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;How Object Relationships are Implemented in POCO Object Models?&lt;/h3&gt;When the multiplicity is one (e.g. 0..1 or 1) the relationship is implemented by defining a &lt;em&gt;navigation property&lt;/em&gt; that reference the other object (e.g. an Address property on User class). When the multiplicity is many (e.g. 0..*, 1..*) the relationship is implemented via an &lt;a href="http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx" target="_blank" mce_href="http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx"&gt;ICollection&lt;/a&gt; of the type of other object. 
&lt;h3&gt;How Relational Database Relationships are Implemented? &lt;/h3&gt;Relationships in relational databases are maintained through the use of &lt;em&gt;Foreign Keys&lt;/em&gt;. A foreign key is a data attribute(s) that appears in one table and must be the primary key or other candidate key in another table. With a one-to-one relationship the foreign key needs to be implemented by one of the tables. To implement a one-to-many relationship we implement a foreign key from the “one table” to the “many table”. We could also choose to implement a one-to-many relationship via an &lt;em&gt;associative table&lt;/em&gt; (aka &lt;em&gt;Join table&lt;/em&gt;), effectively making it a many-to-many relationship. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li style="padding: 0px;"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

&lt;li style="padding: 0px;"&gt;&lt;a href="http://www.agiledata.org/essays/mappingObjects.html" target="_blank" mce_href="http://www.agiledata.org/essays/mappingObjects.html"&gt;Mapping Objects to Relational Databases&lt;/a&gt; &lt;/li&gt;

&lt;li style="padding: 0px;"&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank" mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;
    a
    {
        color: #5A99FF;
    }
        
    a:visited
    {
        color: #5A99FF;
    }
        
    h3
    {
        border-bottom: 1px solid #DDDDDD;
        color: #333333;
        font-size: 14px;
        font-family: Segoe UI,arial,serif;
        padding-bottom: 0.2em;
        font-weight: normal;
        line-height: 22px;
        padding-top: 10px; /*margin: 33px 0 11px;*/
    }
        
    td
    {
        line-height: 18px;
    }
        
    li
    {
        line-height: 25px;
    }
        
    .code, .typeName
    {
        font-family: consolas;
    }
        
    .typeName
    {
        color: #2b91af;
    }
    .myComment
    {
        border-bottom:#cdcdcd 1px solid;
        border-left:#cdcdcd 1px solid;
        line-height:18px;
        background-color:#f2f2f2;
        border-top:#cdcdcd 1px solid;
        border-right:#cdcdcd 1px solid;
        padding-top:9px;
        padding-left:9px;
        padding-right:9px;
        padding-bottom:9px;
    }
    &lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7734053" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="Entity Framework 4.1" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework+4.1/default.aspx" /></entry><entry><title>Associations in EF Code First CTP5: Part 3 – One-to-One Foreign Key Associations</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx</id><published>2011-01-23T20:22:00Z</published><updated>2011-01-23T20:22:00Z</updated><content type="html">&lt;TABLE style="FONT-FAMILY: arial; FONT-SIZE: 10pt" border=0 cellSpacing=0 cellPadding=0&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;This is the third post in a series that explains entity association mappings with EF Code First. I've described these association types so far: 
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx" target=_blank&gt;Part 1 – Complex Types&lt;/A&gt;&lt;/LI&gt;
&lt;LI class=padTop&gt;&lt;A href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" target=_blank&gt;Part 2 – Shared Primary Key Associations&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;In the previous &lt;A href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx" target=_blank&gt;blog post&lt;/A&gt; we saw the limitations of shared primary key association and argued that this type of association is relatively rare and in many schemas, a one-to-one association is represented with a foreign key field and a unique constraint. Today we are going to discuss how this is done by learning about one-to-one foreign key associations. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;Introducing the Revised Model&lt;/H3&gt;In this revised version, each User always have two addresses: one billing address and another one for delivery. The following figure shows the class diagram for this domain model: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/06/one-to-one-fk.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;One-to-One Foreign Key Association&lt;/H3&gt;Instead of &lt;A href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx#SharedPrimaryKeyAssociations" target=_blank&gt;sharing a primary key&lt;/A&gt;, two rows can have a foreign key relationship. One table has a foreign key column that references the primary key of the associated table (The source and target of this foreign key constraint can even be the same table: This is called a &lt;EM&gt;self-referencing&lt;/EM&gt; relationship.). &lt;U&gt;An additional constraint enforces this relationship as a real one to one&lt;/U&gt;. For example, by making the BillingAddressId column unique, we declare that a particular address can be referenced by at most one user, as a billing address. This isn’t as strong as the guarantee from a shared primary key association, which allows a particular address to be referenced by at most one user, period. With several foreign key columns (which is the case in our domain model since we also have a foreign key for DeliveryAddress), we can reference the same address target row several times. But in any case, &lt;U&gt;two users can’t share the same address for the same purpose&lt;/U&gt;. 
&lt;H3&gt;The Object Model&lt;/H3&gt;Let's start by creating an object model for our domain: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;FirstName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;LastName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;BillingAddressId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;DeliveryAddressId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;BillingAddress&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;DeliveryAddress&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}
 
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;AddressId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;PostalCode&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;
}
 
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;&amp;nbsp;:&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbContext&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbSet&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;&amp;nbsp;Users&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbSet&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;gt;&amp;nbsp;Addresses&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;As you can see, User class has introduced two new scalar properties as &lt;EM&gt;BillingAddressId&lt;/EM&gt; and &lt;EM&gt;DeliveryAddressId&lt;/EM&gt; as well as their related navigation properties (&lt;EM&gt;BillingAddress &lt;/EM&gt;and &lt;EM&gt;DeliveryAddress&lt;/EM&gt;). 
&lt;H3&gt;Configuring Foreign Keys With Fluent API&lt;/H3&gt;&lt;EM&gt;BillingAddressId&lt;/EM&gt; and &lt;EM&gt;DeliveryAddressId&lt;/EM&gt; are foreign key scalar properties and representing the actual foreign key values that the relationships are established on. However, Code First will &lt;U&gt;not&lt;/U&gt; recognize them as the foreign keys for the associations since their names are not aligned with the &lt;A href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx" target=_blank&gt;conventions&lt;/A&gt; that it has to infer foreign keys. Therefore, we need to use fluent API (or Data Annotations) to tell Code First about the foreign keys. Here is the fluent API code to identify the foreign key properties: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt;&amp;nbsp;OnModelCreating(&lt;SPAN style="COLOR: #2b91af"&gt;ModelBuilder&lt;/SPAN&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddressId);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddressId);
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Alternatively, we can use Data Annotations to achieve this. CTP5 introduced a new attribute in &lt;A href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx" target=_blank&gt;System.ComponentModel.DataAnnotations&lt;/A&gt; namespace which is called &lt;SPAN class=code&gt;ForeignKeyAttribute&lt;/SPAN&gt; and we can place it on a navigation property to specify the property that represents the foreign key of the relationship: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;FirstName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;LastName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;BillingAddressId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;DeliveryAddressId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;SPAN style="COLOR: #2b91af"&gt;ForeignKey&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"BillingAddressId"&lt;/SPAN&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;BillingAddress&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;SPAN style="COLOR: #2b91af"&gt;ForeignKey&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"DeliveryAddressId"&lt;/SPAN&gt;)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;DeliveryAddress&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;However, we will not use this Data Annotation and will stick with our fluent API code for a reason that you'll see soon. 
&lt;H3&gt;Creating a SQL Server Schema&lt;/H3&gt;The object model seems to be ready to give us the desired SQL schema, however, if we try to create a &lt;U&gt;SQL Server&lt;/U&gt; database from it, we will get an &lt;A class=code href="http://msdn.microsoft.com/en-us/library/system.invalidoperationexception.aspx" target=_blank&gt;InvalidOperationException&lt;/A&gt; with this message: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-BOTTOM: 10px; PADDING-TOP: 10px"&gt;
&lt;DIV style="WIDTH: 640px" class=exception&gt;"The database creation succeeded, but the creation of the database objects did not. See InnerException for details." &lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;The inner exception is a &lt;A class=code href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception(v=VS.100).aspx" target=_blank&gt;System.Data.SqlClient.SqlException&lt;/A&gt; containing this message: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-BOTTOM: 10px; PADDING-TOP: 10px"&gt;
&lt;DIV style="WIDTH: 640px" class=exception&gt;"Introducing FOREIGN KEY constraint 'User_DeliveryAddress' on table 'Users' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors." &lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;As you can tell from the type of the inner exception (SqlException), it has nothing to do with EF or Code First; it has been generated purely by SQL Server when Code First was trying to create a database based on our object model. 
&lt;H3&gt;SQL Server and Multiple Cascade Paths&lt;/H3&gt;A &lt;I&gt;Multiple cascade path&lt;/I&gt; happens when a cascade path goes from column col1 in table A to table B and also from column col2 in table A to table B. So it seems that Code First tried to turn on Cascade Delete for both BillingAddressId and DeliveryAddressId columns in Users table. In fact, Code First was trying to use &lt;A href="http://www.nerdymusings.com/LPMArticle.asp?ID=34" target=_blank&gt;Declarative Referential Integrity (DRI)&lt;/A&gt; to enforce cascade deletes and the problem is that SQL Server is &lt;U&gt;not&lt;/U&gt; fully &lt;A href="http://en.wikipedia.org/wiki/SQL-92" target=_blank&gt;ANSI SQL-92&lt;/A&gt; compliant when it comes to the cascading actions. &lt;U&gt;In SQL Server, DRI forbids cascading updates or deletes in a multiple cascade path scenario&lt;/U&gt;. &lt;BR&gt;&lt;BR&gt;A &lt;A href="http://support.microsoft.com/kb/321843" target=_blank&gt;KB article&lt;/A&gt; also &lt;EM&gt;explains&lt;/EM&gt; why we received this error: &lt;I&gt;"In SQL Server, a table cannot appear more than one time in a list of all the cascading referential actions that are started by either a DELETE or an UPDATE statement. For example, the tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree".&lt;/I&gt; (i.e. the User table appeared twice in a list of cascading referential actions started by a DELETE). Basically, SQL Server does simple counting of cascade paths and, rather than trying to work out whether any cycles actually exist, it assumes the worst and refuses to create the referential actions (Cascades). &lt;BR&gt;&lt;BR&gt;Therefore, depend on our database engine, we may or may not get this exception (For example, both Oracle and MySQL let us create Cascades in this scenario.). 
&lt;H3&gt;Overriding Code First Convention To Resolve the Problem&lt;/H3&gt;As you saw, Code First automatically turns on Cascade Deletes on &lt;EM&gt;required&lt;/EM&gt; one-to-many associations based on the &lt;A href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx" target=_blank&gt;conventions&lt;/A&gt;. However, in order to resolve the exception that we got from SQL Server, we have no choice other than overriding this convention and switching cascade deletes off on at least one of the associations and as of CTP5, the only way to accomplish this is by using fluent API. Let's switch it off on DeliveryAddress Association: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt;&amp;nbsp;OnModelCreating(&lt;SPAN style="COLOR: #2b91af"&gt;ModelBuilder&lt;/SPAN&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddressId);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasRequired(a&amp;nbsp;=&amp;gt;&amp;nbsp;a.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithMany()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasForeignKey(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddressId).&lt;SPAN style="BORDER-BOTTOM: red 1px solid"&gt;WillCascadeOnDelete(&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;)&lt;/SPAN&gt;;
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3 style="PADDING-TOP: 0px"&gt;One-to-One Foreign Key Associations in EF Code First&lt;/H3&gt;As you may have noticed, both associations in the fluent API code has been configured as a &lt;I&gt;many-to-one&lt;/I&gt;—not one-to-one, as you might have expected. The reason is simple: Code First (and EF in general) does &lt;U&gt;not&lt;/U&gt; natively support one-to-one foreign key associations. In fact, EF does not support any association scenario that involves unique constraints at all. Fortunately, in this case we don’t care what’s on the target side of the association, so we can treat it like a &lt;EM&gt;to-one&lt;/EM&gt; association without the &lt;EM&gt;many&lt;/EM&gt; part. All we want is to express “This entity (User) has a property that is a reference to an instance of another entity (Address)” and use a foreign key field to represent that relationship. Basically EF still thinks that the relationship is many-to-one. This is a workaround for the current EF limitation which comes with two consequences: First, EF won't create any additional constraint for us to enforces this relationship as a one to one, we need to manually create it ourselves. The second limitation that this lack of support impose to us is more important: &lt;U&gt;one to one foreign key associations cannot be bidirectional&lt;/U&gt; (i.e. we cannot define a User property on the Address class). 
&lt;H3&gt;Create a Unique Constraint To Enforce the Relationship as a Real One to One&lt;/H3&gt;We can manually create unique constraints on the foreign keys in the database after Code First creates it for us but if you are like me and prefer to create your database in one shot then there is a way in CTP5 to have Code First create the constraints as part of its database creation process. For that we can take advantage of the new CTP5’s &lt;SPAN class=code&gt;SqlCommand&lt;/SPAN&gt; method on &lt;SPAN class=code&gt;DbDatabase&lt;/SPAN&gt; class which allows raw SQL commands to be executed against the database. The best place to invoke &lt;SPAN class=code&gt;SqlCommand&lt;/SPAN&gt; method for this purpose is inside a &lt;SPAN class=code&gt;Seed&lt;/SPAN&gt; method that has been overridden in a custom Initializer class: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt;&amp;nbsp;Seed(&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;&amp;nbsp;context)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Database.SqlCommand(&lt;SPAN style="COLOR: #a31515"&gt;"ALTER&amp;nbsp;TABLE&amp;nbsp;Users&amp;nbsp;ADD&amp;nbsp;CONSTRAINT&amp;nbsp;uc_Billing&amp;nbsp;UNIQUE(BillingAddressId)"&lt;/SPAN&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Database.SqlCommand(&lt;SPAN style="COLOR: #a31515"&gt;"ALTER&amp;nbsp;TABLE&amp;nbsp;Users&amp;nbsp;ADD&amp;nbsp;CONSTRAINT&amp;nbsp;uc_Delivery&amp;nbsp;UNIQUE(DeliveryAddressId)"&lt;/SPAN&gt;);
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;This code adds unique constraints to the BillingAddressId and DeliveryAddressId columns in the DDL generated by Code First. 
&lt;H3&gt;SQL Schema&lt;/H3&gt;The object model is ready now and Code First will create the following database schema for us: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=padTop5&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/06/1-1FkSchema.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class=padTop10&gt;It is worth mentioning that we can still enforce cascade deletes for DeliveryAddress relationship. SQL Server allows enforcing Referential Integrity in two different ways. DRI that we just saw is the most basic yet least flexible way. The other way is to use &lt;EM&gt;Triggers&lt;/EM&gt;. We can write a &lt;A href="http://support.microsoft.com/kb/142480#top" target=_blank&gt;Delete Triggers&lt;/A&gt; on the primary table that either deletes the rows in the dependent table(s) or sets all corresponding foreign keys to NULL (In our case the foreign keys are Non-Nullable so it has to delete the dependent rows). 
&lt;H3&gt;Download&lt;/H3&gt;&lt;A href="http://weblogs.asp.net/blogs/manavi/06/CodeFirst.Associations.OneToOneFK.zip"&gt;Click here&lt;/A&gt; to download and run the one-to-one foreign key association sample that we have built in this blog post. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;Summary&lt;/H3&gt;In this blog post we learned about one-to-one foreign key associations as a better way to represent one to one relationships. However, we saw some limitations such as the need for manual creation of unique constraints and also the fact that these type of associations cannot be bidirectional, all due to the lack of unique constraint support in EF. Support for unique constraints is going to require changes to the whole EF stack and it won't happen in the RTM targeted for this year as that RTM will be layered on top of the current .NET 4.0 functionality. That said, EF team has this feature on their list for the future, so hopefully it will be supported in a later release of EF and until then the workaround that I showed here is going to be the way to implement one-to-one foreign key associations in EF Code First. 
&lt;H3 style="PADDING-BOTTOM: 0px"&gt;References&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/b/adonet/" target=_blank mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.manning.com/bauer2/" target=_blank mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;STYLE type=text/css&gt;        
        a
        {            
            color: #5A99FF;
        }
        
        h3 
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;            
            line-height: 22px;
            padding-top: 10px;
            /*margin: 33px 0 11px;*/
        }
    
        td         
        {
            line-height: 18px;
        }

        a:visited
        {
            color: #5A99FF;
        }
        
        .subTitle
        {
            padding-bottom: 8px;                        
            font-weight: bold;
            padding-top: 15px;
            text-decoration:underline;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
        
        .exception
        {
            background-color: #f0f0f0;
            font-style: italic;
            font-family:Segoe UI;
            padding-bottom: 5px;
            padding-left: 10px;
            padding-top: 5px;
            padding-right: 5px;
            
        }
        
    &lt;/STYLE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7688465" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="CTP5" scheme="http://weblogs.asp.net/manavi/archive/tags/CTP5/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx" /><id>http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx</id><published>2011-01-03T00:32:00Z</published><updated>2011-01-03T00:32:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;This is the third (and last) post in a series that explains different approaches to map an inheritance hierarchy with EF Code First. I've described these strategies in previous posts: 
&lt;ul&gt;
&lt;li&gt;&lt;a style="font-weight: bold;" href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx" target="_blank"&gt;Part 1 – Table per Hierarchy (TPH)&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a style="font-weight: bold;" href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx" target="_blank"&gt;Part 2 – Table per Type (TPT)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
In today’s blog post I am going to discuss Table per Concrete Type (TPC) which completes the inheritance mapping strategies supported by EF Code First. At the end of this post I will provide some guidelines to choose an inheritance strategy mainly based on what we've learned in this series. 
&lt;h3&gt;TPC and Entity Framework in the Past &lt;/h3&gt;Table per Concrete type is somehow the simplest approach suggested, yet using TPC with EF is one of those concepts that has not been covered very well so far and I've seen in some resources that it was even discouraged. The reason for that is just because Entity Data Model Designer in VS2010 doesn't support TPC (even though the EF runtime does). That basically means if you are following EF's Database-First or Model-First approaches then configuring TPC requires manually writing XML in the EDMX file which is not considered to be a fun practice. Well, no more. You'll see that with Code First, creating TPC is perfectly possible with fluent API just like other strategies and you don't need to avoid TPC due to the lack of designer support as you would probably do in other EF approaches. 
&lt;h3&gt;Table per Concrete Type (TPC)&lt;/h3&gt;In Table per Concrete type (aka Table per Concrete class) we use &lt;u&gt;exactly one table&lt;/u&gt; for &lt;u&gt;each (nonabstract) class&lt;/u&gt;. All properties of a class, including inherited properties, can be mapped to columns of this table, as shown in the following figure: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 5px; padding-left: 50px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-left: 5px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/05/TPCSchema.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 15px;"&gt;As you can see, the SQL schema is &lt;u&gt;not&lt;/u&gt; aware of the inheritance; effectively, we’ve mapped two &lt;u&gt;unrelated&lt;/u&gt; tables to a more expressive class structure. If the base class was concrete, then an &lt;u&gt;additional table&lt;/u&gt; would be needed to hold instances of that class. I have to emphasize that there is &lt;u&gt;no relationship&lt;/u&gt; between the database tables, except for the fact that they share some similar columns. 
&lt;h3&gt;TPC Implementation in Code First &lt;/h3&gt;Just like the &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx#TPTImplementation" target="_blank"&gt;TPT implementation&lt;/a&gt;, we need to specify a separate table for each of the subclasses. We also need to tell Code First that we want all of the inherited properties to be mapped as part of this table. In CTP5, there is a new helper method on &lt;span class="code"&gt;EntityMappingConfiguration&lt;/span&gt; class called &lt;span class="code"&gt;MapInheritedProperties&lt;/span&gt; that exactly does this for us. Here is the complete object model as well as the fluent API to create a TPC mapping: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingDetailId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Owner&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Number&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;BankName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Swift&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;CardType&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ExpiryMonth&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ExpiryYear&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;BillingDetails&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;().Map(m&amp;nbsp;=&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m.MapInheritedProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m.ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"BankAccounts"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;gt;().Map(m&amp;nbsp;=&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m.MapInheritedProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;m.ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"CreditCards"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 20px;"&gt;
&lt;table style="border: 1px solid black;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="padding-right: 20px; padding-bottom: 20px; padding-left: 20px;"&gt;
&lt;table border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;div class="title"&gt;The Importance of EntityMappingConfiguration Class&lt;/div&gt;As a side note, it worth mentioning that &lt;span class="code"&gt;EntityMappingConfiguration&lt;/span&gt; class turns out to be a key type for inheritance mapping in Code First. Here is an snapshot of this class: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt;&amp;nbsp;System.Data.Entity.ModelConfiguration.Configuration.Mapping
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;EntityMappingConfiguration&lt;/span&gt;&amp;lt;TEntityType&amp;gt;&amp;nbsp;&lt;span style="color: blue;"&gt;where&lt;/span&gt;&amp;nbsp;TEntityType&amp;nbsp;:&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;ValueConditionConfiguration&lt;/span&gt;&amp;nbsp;Requires(&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;discriminator);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;ToTable(&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;tableName);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;MapInheritedProperties();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;As you have seen so far, we used its &lt;span class="code"&gt;Requires&lt;/span&gt; method to &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx#FluentAPI" target="_blank"&gt;customize TPH&lt;/a&gt;. We also used its &lt;span class="code"&gt;ToTable&lt;/span&gt; method to &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx#TPTImplementation" target="_blank"&gt;create a TPT&lt;/a&gt; and now we are using its &lt;span class="code"&gt;MapInheritedProperties&lt;/span&gt; along with &lt;span class="code"&gt;ToTable&lt;/span&gt; method to create our TPC mapping. &lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 20px;"&gt;TPC Configuration is Not Done Yet!&lt;/h3&gt;We are not quite done with our TPC configuration and there is more into this story even though the fluent API we saw perfectly created a TPC mapping for us in the database. To see why, let's start working with our object model. For example, the following code creates two new objects of BankAccount and CreditCard types and &lt;i&gt;tries&lt;/i&gt; to add them to the database: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;nbsp;bankAccount&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;nbsp;creditCard&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;()&amp;nbsp;{&amp;nbsp;CardType&amp;nbsp;=&amp;nbsp;1&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.BillingDetails.Add(bankAccount);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.BillingDetails.Add(creditCard);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Running this code throws an &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.invalidoperationexception.aspx" target="_blank"&gt;InvalidOperationException&lt;/a&gt; with this message: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px; padding-bottom: 10px;"&gt;
&lt;div style="width: 680px;" class="exception"&gt;The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges. &lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;The reason we got this exception is because &lt;span class="code"&gt;DbContext.SaveChanges()&lt;/span&gt; internally invokes &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/bb336792.aspx" target="_blank"&gt;SaveChanges&lt;/a&gt; method of its internal &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx" target="_blank"&gt;ObjectContext&lt;/a&gt;. ObjectContext's SaveChanges method on its turn by default calls &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.acceptallchanges.aspx" target="_blank"&gt;AcceptAllChanges&lt;/a&gt; after it has performed the database modifications. &lt;span class="code"&gt;AcceptAllChanges&lt;/span&gt; method merely iterates over all entries in &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx" target="_blank"&gt;ObjectStateManager&lt;/a&gt; and invokes &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectstateentry.acceptchanges.aspx" target="_blank"&gt;AcceptChanges&lt;/a&gt; on each of them. Since the entities are in &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx" target="_blank"&gt;Added&lt;/a&gt; state, AcceptChanges method replaces their temporary &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectstateentry.entitykey.aspx" target="_blank"&gt;EntityKey&lt;/a&gt; with a regular EntityKey based on the primary key values (i.e. BillingDetailId) that come back from the database and that's where the problem occurs since both the entities have been assigned the same value for their primary key by the database (i.e. on both BillingDetailId = 1) and the problem is that &lt;span class="code"&gt;ObjectStateManager&lt;/span&gt; cannot track objects of the same type (i.e. BillingDetail) with the same EntityKey value hence it throws. If you take a closer look at the TPC's SQL schema above, you'll see why the database generated the same values for the primary keys: the BillingDetailId column in both BankAccounts and CreditCards table has been marked as identity. 
&lt;h3&gt;How to Solve The Identity Problem in TPC &lt;/h3&gt;As you saw, using SQL Server’s int identity columns doesn't work very well together with TPC since there will be duplicate entity keys when inserting in subclasses tables with all having the same identity seed. Therefore, to solve this, either a spread seed (where each table has its own initial seed value) will be needed, or a mechanism other than SQL Server’s int identity should be used. Some other RDBMSes have other mechanisms allowing a sequence (identity) to be shared by multiple tables, and something similar can be achieved with GUID keys in SQL Server. While using GUID keys, or int identity keys with different starting seeds will solve the problem but yet another solution would be to completely switch off identity on the primary key property. As a result, we need to take the responsibility of providing unique keys when inserting records to the database. We will go with this solution since it works regardless of which database engine is used. 
&lt;h3&gt;Switching Off Identity in Code First &lt;/h3&gt;We can switch off identity simply by placing &lt;span class="code"&gt;DatabaseGenerated&lt;/span&gt; attribute on the primary key property and pass &lt;span class="code"&gt;DatabaseGenerationOption.None&lt;/span&gt; to its constructor. &lt;span class="code"&gt;DatabaseGenerated&lt;/span&gt; attribute is a new data annotation which has been added to &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx" target="_blank"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt; namespace in CTP5: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[&lt;span style="color: rgb(43, 145, 175);"&gt;DatabaseGenerated&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;DatabaseGenerationOption&lt;/span&gt;.None)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingDetailId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Owner&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Number&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;As always, we can achieve the same result by using fluent API, if you prefer that: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Property(p&amp;nbsp;=&amp;gt;&amp;nbsp;p.BillingDetailId)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.HasDatabaseGenerationOption(&lt;span style="color: rgb(43, 145, 175);"&gt;DatabaseGenerationOption&lt;/span&gt;.None);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 5px;"&gt;Working With The Object Model &lt;/h3&gt;Our TPC mapping is ready and we can try adding new records to the database. But, like I said, now we need to take care of providing unique keys when creating new objects: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;nbsp;bankAccount&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;()&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BillingDetailId&amp;nbsp;=&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;nbsp;creditCard&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;()&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BillingDetailId&amp;nbsp;=&amp;nbsp;2,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CardType&amp;nbsp;=&amp;nbsp;1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.BillingDetails.Add(bankAccount);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.BillingDetails.Add(creditCard);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;


&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;Polymorphic Associations with TPC is Problematic &lt;/h3&gt;The main problem with this approach is that it doesn’t support &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx#PolymorphicAssociations" target="_blank"&gt;Polymorphic Associations&lt;/a&gt; very well. After all, in the database, associations are represented as foreign key relationships and in TPC, the subclasses are all mapped to different tables so a polymorphic association to their base class (abstract BillingDetail in our example) cannot be represented as a simple foreign key relationship. For example, consider the domain model we introduced &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx#PolymorphicAssociations" target="_blank"&gt;here&lt;/a&gt; where User has a polymorphic association with BillingDetail. This would be problematic in our TPC Schema, because if User has a many-to-one relationship with BillingDetail, the Users table would need a single foreign key column, which would have to refer both concrete subclass tables. This isn’t possible with regular foreign key constraints. 
&lt;h3&gt;Schema Evolution with TPC is Complex &lt;/h3&gt;A further conceptual problem with this mapping strategy is that several different columns, of different tables, share exactly the same semantics. This makes schema evolution more complex. For example, a change to a base class property results in changes to multiple columns. It also makes it much more difficult to implement database integrity constraints that apply to all subclasses. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Generated SQL&lt;/h3&gt;Let's examine SQL output for polymorphic queries in TPC mapping. For example, consider this polymorphic query for all BillingDetails and the resulting SQL statements that being executed in the database: &lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;query&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;from&lt;/span&gt;&amp;nbsp;b&amp;nbsp;&lt;span style="color: blue;"&gt;in&lt;/span&gt;&amp;nbsp;context.BillingDetails&amp;nbsp;&lt;span style="color: blue;"&gt;select&lt;/span&gt;&amp;nbsp;b;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 5px;"&gt;&lt;img style="border-top-color: rgb(160, 160, 160); border-bottom-color: rgb(160, 160, 160); border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-bottom-style: solid;" src="http://weblogs.asp.net/blogs/manavi/05/tpcpolymorphic.png"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 20px;"&gt;Just like the &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx#GeneratedSQL" target="_blank"&gt;SQL query generated &lt;/a&gt;by TPT mapping, the &lt;a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blank"&gt;CASE&lt;/a&gt; statements that you see in the beginning of the query is merely to ensure columns that are irrelevant for a particular row have NULL values in the returning flattened table. (e.g. BankName for a row that represents a CreditCard type). 
&lt;h3&gt;TPC's SQL Queries are Union Based &lt;/h3&gt;As you can see in the above screenshot, the first SELECT uses a FROM-clause subquery (which is selected with a red rectangle) to retrieve all instances of BillingDetails from all concrete class tables. The tables are combined with a &lt;a href="http://msdn.microsoft.com/en-us/library/ms180026.aspx" target="_blank"&gt;UNION&lt;/a&gt; operator, and a literal (in this case, 0 and 1) is inserted into the intermediate result; (look at the lines highlighted in yellow.) EF reads this to instantiate the correct class given the data from a particular row. A union requires that the queries that are combined, project over the same columns; hence, EF has to pad and fill up nonexistent columns with NULL. This query will really perform well since here we can let the database optimizer find the best execution plan to combine rows from several tables. There is also no Joins involved so it has a better performance than the SQL queries generated by TPT where a Join is required between the base and subclasses tables. 
&lt;h3&gt;Choosing Strategy Guidelines&lt;/h3&gt;Before we get into this discussion, I want to emphasize that there is no one single "best strategy fits all scenarios" exists. As you saw, each of the approaches have their own advantages and drawbacks. Here are some rules of thumb to identify the best strategy in a particular scenario: 
&lt;ul&gt;
&lt;li class="padTop"&gt;If you don’t require polymorphic associations or queries, lean toward &lt;strong&gt;TPC&lt;/strong&gt;—in other words, if you never or rarely query for BillingDetails and you have no class that has an association to BillingDetail base class. I recommend TPC (only) for the &lt;u&gt;top level of your class hierarchy&lt;/u&gt;, where polymorphism isn’t usually required, and when modification of the base class in the future is unlikely. &lt;/li&gt;

&lt;li class="padTop10"&gt;If you do require polymorphic associations or queries, and subclasses declare relatively few properties (particularly if the main difference between subclasses is in their behavior), lean toward &lt;strong&gt;TPH&lt;/strong&gt;. Your goal is to minimize the number of nullable columns and to convince yourself (and your DBA) that a denormalized schema won’t create problems in the long run. &lt;/li&gt;

&lt;li class="padTop10"&gt;If you do require polymorphic associations or queries, and subclasses declare many properties (subclasses differ mainly by the data they hold), lean toward &lt;strong&gt;TPT&lt;/strong&gt;. Or, depending on the width and depth of your inheritance hierarchy and the possible cost of joins versus unions, use &lt;b&gt;TPC&lt;/b&gt;. &lt;/li&gt;
&lt;/ul&gt;
By default, choose TPH only for simple problems. For more complex cases (or when you’re overruled by a data modeler insisting on the importance of nullability constraints and normalization), you should consider the TPT strategy. But at that point, ask yourself whether it may not be better to remodel inheritance as delegation in the object model (&lt;i&gt;delegation&lt;/i&gt; is a way of making composition as powerful for reuse as inheritance). Complex inheritance is often best avoided for all sorts of reasons unrelated to persistence or ORM. EF acts as a buffer between the domain and relational models, but that doesn’t mean you can ignore persistence concerns when designing your classes. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Summary&lt;/h3&gt;In this series, we focused on one of the main structural aspect of the object/relational paradigm mismatch which is inheritance and discussed how EF solve this problem as an ORM solution. We learned about the three well-known inheritance mapping strategies and their implementations in EF Code First. Hopefully it gives you a better insight about the mapping of inheritance hierarchies as well as choosing the best strategy for your particular scenario. &lt;br&gt;&lt;br&gt;Happy New Year and Happy Code-Firsting! &lt;br&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

&lt;li&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank" mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;
        a
        {
            color: #5A99FF;
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3 
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;            
            line-height: 22px;
            padding-top: 10px;
            /*margin: 33px 0 11px;*/
        }
    
        td         
        {
            line-height: 18px;
        }
        
        .title
        {
            padding-bottom: 14px;
            font-family: Segoe UI;
            font-size: 14px;
            font-weight: ;
            padding-top: 18px;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
        
        .exception
        {
            background-color: #f0f0f0;
            font-style: italic;
            padding-bottom: 5px;
            padding-left: 5px;
            padding-top: 5px;
            padding-right: 5px;
        }
    .myComment
    {
        border-bottom:#cdcdcd 1px solid;
        border-left:#cdcdcd 1px solid;
        padding-bottom:9px;
        line-height:18px;
        background-color:#f2f2f2;
        padding-left:9px;
        padding-right:9px;
        border-top:#cdcdcd 1px solid;
        border-right:#cdcdcd 1px solid;
        padding-top:9px;
    }
    &lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7672453" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="CTP5" scheme="http://weblogs.asp.net/manavi/archive/tags/CTP5/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Inheritance with EF Code First: Part 2 – Table per Type (TPT)</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx" /><id>http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx</id><published>2010-12-28T23:41:00Z</published><updated>2010-12-28T23:41:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;In the previous &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx" target="_blank"&gt;blog post&lt;/a&gt; you saw that there are three different approaches to representing an inheritance hierarchy and I explained Table per Hierarchy (TPH) as the default mapping strategy in EF Code First. We argued that the disadvantages of TPH may be too serious for our design since it results in denormalized schemas that can become a major burden in the long run. In today’s blog post we are going to learn about Table per Type (TPT) as another inheritance mapping strategy and we'll see that TPT doesn’t expose us to this problem. 
&lt;h3&gt;Table per Type (TPT)&lt;/h3&gt;&lt;i&gt;Table per Type&lt;/i&gt; is about representing inheritance relationships as &lt;u&gt;relational foreign key associations&lt;/u&gt;. Every class/subclass that declares persistent properties—including abstract classes—has its own table. The table for &lt;u&gt;subclasses&lt;/u&gt; contains columns only for each &lt;u&gt;noninherited&lt;/u&gt; property (each property declared by the subclass itself) along with a primary key that is also a foreign key of the base class table. This approach is shown in the following figure: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 5px; padding-left: 70px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-left: 0px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/04/TPTSchema.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;For example, if an instance of the CreditCard subclass is made persistent, the values of properties declared by the BillingDetail base class are persisted to a new row of the BillingDetails table. Only the values of properties declared by the subclass (i.e. CreditCard) are persisted to a new row of the CreditCards table. The two rows are linked together by their &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" target="_blank"&gt;shared primary key value&lt;/a&gt;. Later, the subclass instance may be retrieved from the database by joining the subclass table with the base class table. 
&lt;h3&gt;TPT Advantages &lt;/h3&gt;The primary advantage of this strategy is that the &lt;u&gt;SQL schema is normalized&lt;/u&gt;. In addition, schema evolution is straightforward (modifying the base class or adding a new subclass is just a matter of modify/add one table). Integrity constraint definition are also straightforward (note how CardType in CreditCards table is now a non-nullable column). 
&lt;h3&gt;&lt;a title="TPTImplementation" name="TPTImplementation"&gt;&lt;/a&gt;Implement TPT in EF Code First&lt;/h3&gt;We can create a TPT mapping simply by placing &lt;span class="code"&gt;Table&lt;/span&gt; attribute on the subclasses to specify the mapped table name (&lt;span class="code"&gt;Table&lt;/span&gt; attribute is a new data annotation and has been added to &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx" target="_blank"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt; namespace in CTP5): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingDetailId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Owner&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Number&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
[&lt;span style="color: rgb(43, 145, 175);"&gt;Table&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"BankAccounts"&lt;/span&gt;)]
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;BankName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Swift&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
[&lt;span style="color: rgb(43, 145, 175);"&gt;Table&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"CreditCards"&lt;/span&gt;)]
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;CardType&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ExpiryMonth&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ExpiryYear&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;BillingDetails&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="padTop10"&gt;If you prefer &lt;strong&gt;fluent API&lt;/strong&gt;, then you can create a TPT mapping by using &lt;span class="code"&gt;ToTable()&lt;/span&gt; method: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;().ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"BankAccounts"&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;gt;().ToTable(&lt;span style="color: rgb(163, 21, 21);"&gt;"CreditCards"&lt;/span&gt;);
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;&lt;a title="PolymorphicAssociations" name="PolymorphicAssociations"&gt;&lt;/a&gt;Polymorphic Associations&lt;/h3&gt;A &lt;i&gt;polymorphic association&lt;/i&gt; is an association to a base class, hence to all classes in the hierarchy with dynamic resolution of the concrete class at runtime. For example, consider the BillingInfo property of User in the following domain model. It references one particular BillingDetail object, which at runtime can be any concrete instance of that class. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 15px; padding-bottom: 15px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/04/PolymorphicAssociationModel.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;In fact, because BillingDetail is abstract, the association must refer to an instance of one of its subclasses only—CreditCard or BankAccount—at runtime. 
&lt;h3&gt;Implement Polymorphic Associations with EF Code First &lt;/h3&gt;We don’t have to do anything special to enable polymorphic associations in EF Code First; The user needs a unidirectional association to some BillingDetails, which can be CreditCard or BankAccount so we just create this association and it would be naturally polymorphic: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;

    &lt;pre style="background: white; color: black; font-family: Consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;FirstName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;LastName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingDetailId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;virtual&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;nbsp;BillingInfo&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;

&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;In other words, as you can see above, a polymorphic association is an association that may refer instances of a subclass of the class that was explicitly specified as the type of the navigation property (e.g. User.BillingInfo). &lt;br&gt;&lt;br&gt;The following code demonstrates the creation of an association to an instance of the CreditCard subclass: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;nbsp;creditCard&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Number&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;"987654321"&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CardType&amp;nbsp;=&amp;nbsp;1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UserId&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;1,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BillingInfo&amp;nbsp;=&amp;nbsp;creditCard
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Add(user);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Now, if we navigate the association in a second context, EF Code First automatically retrieves the CreditCard instance: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;User&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;context.Users.Find(1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Assert(user.BillingInfo&amp;nbsp;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;);
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3 style="padding-top: 0px;"&gt;Polymorphic Associations with TPT &lt;/h3&gt;Another important advantage of TPT is the ability to handle polymorphic associations. In the database a polymorphic association to a particular base class will be represented as a foreign key referencing the table of that particular base class. (e.g. Users table has a foreign key that references BillingDetails table.) &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Generated SQL For Queries&lt;a title="GeneratedSQL" name="GeneratedSQL"&gt;&lt;/a&gt;&amp;nbsp;&lt;/h3&gt;Let’s take an example of a simple non-polymorphic query that returns a list of all the BankAccounts: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;query&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;from&lt;/span&gt;&amp;nbsp;b&amp;nbsp;&lt;span style="color: blue;"&gt;in&lt;/span&gt;&amp;nbsp;context.BillingDetails.OfType&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;()&amp;nbsp;&lt;span style="color: blue;"&gt;select&lt;/span&gt;&amp;nbsp;b;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Executing this query (by invoking &lt;span class="code"&gt;ToList()&lt;/span&gt; method) results in the following SQL statements being sent to the database (on the bottom, you can also see the result of executing the generated query in SQL Server Management Studio): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 15px;"&gt;&lt;img style="border-top-color: rgb(160, 160, 160); border-top-width: 1px; border-top-style: solid;" src="http://weblogs.asp.net/blogs/manavi/04/NonPolymorphicQuery.png"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Now, let’s take an example of a very simple polymorphic query that requests all the BillingDetails which includes both BankAccount and CreditCard types: &lt;/td&gt;
&lt;/tr&gt;
&lt;!--
&lt;tr&gt;
                
&lt;td&gt;
                    projects some properties out of the base class BillingDetail, without querying for anything from any of the subclasses:
                    
&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&amp;nbsp;query&amp;nbsp;=&amp;nbsp;&lt;span 
                        style="color: blue"&gt;from&lt;/span&gt;&amp;nbsp;b&amp;nbsp;&lt;span style="color: blue"&gt;in&lt;/span&gt;&amp;nbsp;context.BillingDetails
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue"&gt;select&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;{&amp;nbsp;b.BillingDetailId,&amp;nbsp;b.Number,&amp;nbsp;b.Owner&amp;nbsp;};&lt;/pre&gt;
                    
                &lt;/td&gt;

            &lt;/tr&gt;

--&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&amp;nbsp;query&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;from&lt;/span&gt;&amp;nbsp;b&amp;nbsp;&lt;span style="color: blue;"&gt;in&lt;/span&gt;&amp;nbsp;context.BillingDetails&amp;nbsp;&lt;span style="color: blue;"&gt;select&lt;/span&gt;&amp;nbsp;b;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;This LINQ query seems even more simple than the previous one but the resulting SQL query is not as simple as you might expect: &lt;/td&gt;
&lt;/tr&gt;
&lt;!--
&lt;tr&gt;
                
&lt;td style="padding-top:20px;"&gt;
                    &lt;img src="http://weblogs.asp.net/blogs/manavi/04/polymorphic1.png"&gt;
                &lt;/td&gt;

            &lt;/tr&gt;

            --&gt;
&lt;tr&gt;
&lt;td style="padding-top: 15px;"&gt;&lt;img style="border-top-color: rgb(160, 160, 160); border-bottom-color: rgb(160, 160, 160); border-top-width: 1px; border-bottom-width: 1px; border-top-style: solid; border-bottom-style: solid;" src="http://weblogs.asp.net/blogs/manavi/04/polymorphic2.png"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 20px;"&gt;As you can see, EF Code First relies on an &lt;a href="http://msdn.microsoft.com/en-us/library/ms190014.aspx" target="_blank"&gt;INNER JOIN&lt;/a&gt; to detect the existence (or absence) of rows in the subclass tables CreditCards and BankAccounts so it can determine the concrete subclass for a particular row of the BillingDetails table. Also the SQL &lt;a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blank"&gt;CASE&lt;/a&gt; statements that you see in the beginning of the query is just to ensure columns that are irrelevant for a particular row have NULL values in the returning flattened table. (e.g. BankName for a row that represents a CreditCard type) 
&lt;h3&gt;TPT Considerations&lt;/h3&gt;Even though this mapping strategy is deceptively simple, the experience shows that performance can be unacceptable for complex class hierarchies because queries always require a join across many tables. In addition, this mapping strategy is more difficult to implement by hand— even ad-hoc reporting is more complex. This is an important consideration if you plan to use handwritten SQL in your application (For ad hoc reporting, database views provide a way to offset the complexity of the TPT strategy. A view may be used to transform the table-per-type model into the much simpler table-per-hierarchy model.) &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Summary&lt;/h3&gt;In this post we learned about Table per Type as the second inheritance mapping in our series. So far, the strategies we’ve discussed require extra consideration with regard to the SQL schema (e.g. in TPT, foreign keys are needed). This situation changes with the Table per Concrete Type (TPC) that we will discuss in the next post. &lt;br&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li style="padding-top: 0px;"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

&lt;li&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank" mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;
        a
        {            
            color: #5A99FF;
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3 
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;            
            line-height: 22px;
            padding-top: 10px;
            /*margin: 33px 0 11px;*/
        }
    
        td         
        {
            line-height: 18px;
        }
        
        .title
        {
            padding-bottom: 5px;
            font-family: Segoe UI;
            font-size: 11pt;
            font-weight: bold;
            padding-top: 15px;
        }
    .myComment
    {
        border-bottom:#cdcdcd 1px solid;
        border-left:#cdcdcd 1px solid;
        padding-bottom:9px;
        line-height:18px;
        background-color:#f2f2f2;
        padding-left:9px;
        padding-right:9px;
        border-top:#cdcdcd 1px solid;
        border-right:#cdcdcd 1px solid;
        padding-top:9px;
    }        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
    
    .myComment
    {
        border-bottom:#cdcdcd 1px solid;
        border-left:#cdcdcd 1px solid;
        line-height:18px;
        background-color:#f2f2f2;
        border-top:#cdcdcd 1px solid;
        border-right:#cdcdcd 1px solid;
        padding-top:9px;
        padding-left:9px;
        padding-right:9px;
        padding-bottom:9px;
    }
        p.MsoNormal
        {
            margin-top: 0in;
            margin-right: 0in;
            margin-bottom: 10.0pt;
            margin-left: 0in;
            line-height: 115%;
            font-size: 11.0pt;
            font-family: "Calibri" , "sans-serif";
        }
    &lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7669726" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="CTP5" scheme="http://weblogs.asp.net/manavi/archive/tags/CTP5/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx" /><id>http://weblogs.asp.net/manavi/archive/2010/12/24/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph.aspx</id><published>2010-12-24T04:47:00Z</published><updated>2010-12-24T04:47:00Z</updated><content type="html">&lt;table style="font-family: arial; font-size: 10pt;" border="0" cellSpacing="0" cellPadding="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A simple strategy for mapping classes to database tables might be “one table for every entity persistent class.” This approach sounds simple enough and, indeed, works well until we encounter inheritance. Inheritance is such a visible structural mismatch between the object-oriented and relational worlds because object-oriented systems model both “&lt;i&gt;is a&lt;/i&gt;” and “&lt;i&gt;has a&lt;/i&gt;” relationships. SQL-based models provide only "has a" relationships between entities; SQL database management systems don’t support type inheritance—and even when it’s available, it’s usually proprietary or incomplete. &lt;br&gt;&lt;br&gt;There are three different approaches to representing an inheritance hierarchy: 
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Table per Hierarchy (TPH):&lt;/b&gt; Enable polymorphism by denormalizing the SQL schema, and utilize a type discriminator column that holds type information.&lt;/li&gt;

&lt;li class="padTop5"&gt;&lt;b&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/28/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt.aspx" target="_blank"&gt;Table per Type (TPT):&lt;/a&gt;&lt;/b&gt; Represent "is a" (inheritance) relationships as "has a" (foreign key) relationships.&lt;/li&gt;

&lt;li class="padTop5"&gt;&lt;b&gt;&lt;a href="http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines.aspx" target="_blank"&gt;Table per Concrete class (TPC):&lt;/a&gt;&lt;/b&gt; Discard polymorphism and inheritance relationships completely from the SQL schema.&lt;/li&gt;
&lt;/ul&gt;
I will explain each of these strategies in a series of posts and this one is dedicated to TPH. In this series we'll deeply dig into each of these strategies and will learn about "why" to choose them as well as "how" to implement them. Hopefully it will give you a better idea about which strategy to choose in a particular scenario. 
&lt;h3&gt;Inheritance Mapping with Entity Framework Code First&lt;/h3&gt;All of the inheritance mapping strategies that we discuss in this series will be implemented by EF Code First CTP5. The CTP5 build of the new EF Code First library has been &lt;a href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-released.aspx" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-released.aspx"&gt;released&lt;/a&gt; by ADO.NET team earlier this month. EF Code-First enables a pretty powerful code-centric development workflow for working with data. I’m a big fan of the EF Code First approach, and I’m pretty excited about a lot of productivity and power that it brings. When it comes to inheritance mapping, not only Code First fully supports all the strategies but also gives you ultimate flexibility to work with domain models that involves inheritance. The fluent API for inheritance mapping in CTP5 has been improved a lot and now it's more intuitive and concise in compare to CTP4. 
&lt;h3&gt;A Note For Those Who Follow Other Entity Framework Approaches&lt;/h3&gt;If you are following EF's "Database First" or "Model First" approaches, I still recommend to read this series since although the implementation is Code First specific but the explanations around each of the strategies is perfectly applied to all approaches be it Code First or others. 
&lt;h3&gt;A Note For Those Who are New to Entity Framework and Code-First&lt;/h3&gt;If you choose to learn EF you've chosen well. If you choose to learn EF with Code First you've done even better. To get started, you can find a great walkthrough by Scott Guthrie &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx" target="_blank" mce_href="http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx"&gt;here&lt;/a&gt; and another one by ADO.NET team &lt;a href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx"&gt;here&lt;/a&gt;. In this post, I assume you already setup your machine to do Code First development and also that you are familiar with Code First fundamentals and basic concepts. You might also want to check out my other posts on EF Code First like &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx"&gt;Complex Types&lt;/a&gt; and &lt;a href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" target="_blank" mce_href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx"&gt;Shared Primary Key Associations&lt;/a&gt;. 
&lt;h3&gt;A Top Down Development Scenario&lt;/h3&gt;These posts take a &lt;i&gt;top-down approach&lt;/i&gt;; it assumes that you’re starting with a domain model and trying to derive a new SQL schema. Therefore, we start with an existing domain model, implement it in C# and then let Code First create the database schema for us. However, the mapping strategies described are just as relevant if you’re working bottom up, starting with existing database tables. I’ll show some tricks along the way that help you dealing with nonperfect table layouts. &lt;!--&lt;BR&gt;&lt;BR&gt;Let’s start with the mapping of entity inheritance. --&gt;
&lt;h3&gt;The Domain Model&lt;/h3&gt;In our domain model, we have a &lt;i&gt;BillingDetail&lt;/i&gt; base class which is abstract (note the italic font on the UML class diagram below). We do allow various billing types and represent them as subclasses of BillingDetail class. As for now, we support &lt;i&gt;CreditCard&lt;/i&gt; and &lt;i&gt;BankAccount&lt;/i&gt;: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Implement the Object Model with Code First &lt;/h3&gt;As always, we start with the POCO classes. Note that in our DbContext, I only define one &lt;span class="code"&gt;DbSet&lt;/span&gt; for the base class which is BillingDetail. Code First will find the other classes in the hierarchy based on &lt;i&gt;Reachability Convention.&lt;/i&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;abstract&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;nbsp;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;BillingDetailId&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Owner&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Number&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;BankName&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;Swift&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&amp;nbsp;CardType&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ExpiryMonth&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;ExpiryYear&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}
 
&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;InheritanceMappingContext&lt;/span&gt;&amp;nbsp;:&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbContext&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: rgb(43, 145, 175);"&gt;DbSet&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;BillingDetails&amp;nbsp;{&amp;nbsp;&lt;span style="color: blue;"&gt;get&lt;/span&gt;;&amp;nbsp;&lt;span style="color: blue;"&gt;set&lt;/span&gt;;&amp;nbsp;}
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;This object model is all that is needed to enable inheritance with Code First. If you put this in your application you would be able to immediately start working with the database and do CRUD operations. Before going into details about how EF Code First maps this object model to the database, we need to learn about one of the core concepts of inheritance mapping: polymorphic and non-polymorphic queries. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Polymorphic Queries &lt;/h3&gt;LINQ to Entities and EntitySQL, as object-oriented query languages, both support &lt;i&gt;polymorphic queries&lt;/i&gt;—that is, queries for instances of a class and all instances of its subclasses, respectively. For example, consider the following query: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;linqQuery&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;from&lt;/span&gt;&amp;nbsp;b&amp;nbsp;&lt;span style="color: blue;"&gt;in&lt;/span&gt;&amp;nbsp;context.BillingDetails&amp;nbsp;&lt;span style="color: blue;"&gt;select&lt;/span&gt;&amp;nbsp;b;
&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;billingDetails&amp;nbsp;=&amp;nbsp;linqQuery.ToList();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Or the same query in EntitySQL: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;eSqlQuery&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;@"SELECT&amp;nbsp;VAlUE&amp;nbsp;b&amp;nbsp;FROM&amp;nbsp;BillingDetails&amp;nbsp;AS&amp;nbsp;b"&lt;/span&gt;;
&lt;span style="color: rgb(43, 145, 175);"&gt;ObjectContext&lt;/span&gt;&amp;nbsp;objectContext&amp;nbsp;=&amp;nbsp;((&lt;span style="color: rgb(43, 145, 175);"&gt;IObjectContextAdapter&lt;/span&gt;)context).ObjectContext;
&lt;span style="color: rgb(43, 145, 175);"&gt;ObjectQuery&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;objectQuery&amp;nbsp;=&amp;nbsp;objectContext.CreateQuery&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;(eSqlQuery);
&lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;&amp;nbsp;billingDetails&amp;nbsp;=&amp;nbsp;objectQuery.ToList();&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;span class="code"&gt;linqQuery&lt;/span&gt; and &lt;span class="code"&gt;eSqlQuery&lt;/span&gt; are both polymorphic and return a list of objects of the &lt;u&gt;type&lt;/u&gt; BillingDetail, which is an abstract class but the actual concrete objects in the list are of the subtypes of BillingDetail: CreditCard and BankAccount. 
&lt;h3&gt;Non-polymorphic Queries&lt;/h3&gt;All LINQ to Entities and EntitySQL queries are polymorphic which return not only instances of the specific entity class to which it refers, but all subclasses of that class as well. On the other hand, &lt;i&gt;Non-polymorphic queries&lt;/i&gt; are queries whose polymorphism is restricted and only returns instances of a particular subclass. In LINQ to Entities, this can be specified by using &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/bb344857.aspx" target="_blank"&gt;OfType&amp;lt;T&amp;gt;()&lt;/a&gt; Method. For example, the following query returns only instances of BankAccount: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;&amp;nbsp;query&amp;nbsp;=&amp;nbsp;&lt;span style="color: blue;"&gt;from&lt;/span&gt;&amp;nbsp;b&amp;nbsp;&lt;span style="color: blue;"&gt;in&lt;/span&gt;&amp;nbsp;context.BillingDetails.OfType&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;()&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;select&lt;/span&gt;&amp;nbsp;b;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="padTop5"&gt;EntitySQL has &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/bb399295(v=VS.100).aspx" target="_blank"&gt;OFTYPE&lt;/a&gt; operator that does the same thing: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;eSqlQuery&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;@"SELECT&amp;nbsp;VAlUE&amp;nbsp;b&amp;nbsp;FROM&amp;nbsp;OFTYPE(BillingDetails,&amp;nbsp;Model.BankAccount)&amp;nbsp;AS&amp;nbsp;b"&lt;/span&gt;;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="padTop10"&gt;In fact, the above query with &lt;span class="code"&gt;OFTYPE&lt;/span&gt; operator is a short form of the following query expression that uses &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/bb387106(v=VS.100).aspx" target="_blank"&gt;TREAT&lt;/a&gt; and &lt;a class="code" href="http://msdn.microsoft.com/en-us/library/bb387105(v=VS.100).aspx" target="_blank"&gt;IS OF&lt;/a&gt; operators: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;nbsp;eSqlQuery&amp;nbsp;=&amp;nbsp;&lt;span style="color: rgb(163, 21, 21);"&gt;@"SELECT&amp;nbsp;VAlUE&amp;nbsp;TREAT(b&amp;nbsp;as&amp;nbsp;Model.BankAccount)&amp;nbsp;&lt;/span&gt;
&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FROM&amp;nbsp;BillingDetails&amp;nbsp;AS&amp;nbsp;b&amp;nbsp;&lt;/span&gt;
&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE&amp;nbsp;b&amp;nbsp;IS&amp;nbsp;OF(Model.BankAccount)"&lt;/span&gt;;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;(Note that in the above query, &lt;i&gt;Model.BankAccount&lt;/i&gt; is the fully qualified name for BankAccount class. You need to change "Model" with your own namespace name.) &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Table per Hierarchy (TPH)&lt;/h3&gt;An entire class hierarchy can be mapped to a single table. This table includes columns for &lt;u&gt;all properties&lt;/u&gt; of &lt;u&gt;all classes&lt;/u&gt; in the hierarchy. The concrete subclass represented by a particular row is identified by the value of a &lt;u&gt;type discriminator column&lt;/u&gt;. You don’t have to do anything special in Code First to enable TPH. It's the default inheritance mapping strategy: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/03/BillingModel.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-left: 50px;"&gt;&lt;img src="http://weblogs.asp.net/blogs/manavi/03/TPHSchema.jpg"&gt; &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="padTop10"&gt;This mapping strategy is a winner in terms of both &lt;u&gt;performance&lt;/u&gt; and &lt;u&gt;simplicity&lt;/u&gt;. It’s the best-performing way to represent polymorphism—both polymorphic and nonpolymorphic queries perform well—and it’s even easy to implement by hand. Ad-hoc reporting is possible without complex joins or unions. Schema evolution is straightforward. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Discriminator Column &lt;/h3&gt;As you can see in the DB schema above, Code First has to add a special column to distinguish between persistent classes: the discriminator. This isn’t a property of the persistent class in our object model; it’s used internally by EF Code First. By default, the column name is "Discriminator", and its type is string. The values defaults to the persistent class names —in this case, “BankAccount” or “CreditCard”. EF Code First automatically sets and retrieves the discriminator values. &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;TPH Requires Properties in SubClasses to be Nullable in the Database &lt;/h3&gt;TPH has one major problem: Columns for properties declared by subclasses will be nullable in the database. For example, Code First created an (INT, NULL) column to map CardType property in CreditCard class. However, in a typical mapping scenario, Code First always creates an (INT, NOT NULL) column in the database for an &lt;span class="code"&gt;int&lt;/span&gt; property in persistent class. But in this case, since BankAccount instance won’t have a CardType property, the CardType field must be NULL for that row so Code First creates an (INT, NULL) instead. If your subclasses each define several non-nullable properties, the loss of NOT NULL constraints may be a serious problem from the point of view of data integrity. 
&lt;h3&gt;TPH Violates the Third Normal Form&lt;/h3&gt;Another important issue is normalization. We’ve created &lt;a href="http://en.wikipedia.org/wiki/Functional_dependency" target="_blank"&gt;functional dependencies &lt;/a&gt;between nonkey columns, violating the &lt;a href="http://en.wikipedia.org/wiki/Third_normal_form" target="_blank"&gt;third normal form&lt;/a&gt;. Basically, the value of Discriminator column determines the corresponding values of the columns that belong to the subclasses (e.g. BankName) but Discriminator is &lt;i&gt;not&lt;/i&gt; part of the primary key for the table. As always, denormalization for performance can be misleading, because it sacrifices long-term stability, maintainability, and the integrity of data for immediate gains that may be also achieved by proper optimization of the SQL execution plans (in other words, ask your DBA). &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Generated SQL Query&lt;/h3&gt;Let's take a look at the SQL statements that EF Code First sends to the database when we write queries in LINQ to Entities or EntitySQL. For example, the polymorphic query for BillingDetails that you saw, generates the following SQL statement: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;
&lt;table cellSpacing="0" cellPadding="0" width="490"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="padding-left: 20px; background-color: rgb(240, 240, 240);"&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;SELECT&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Discriminator]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Discriminator]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[BillingDetailId]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[BillingDetailId]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Owner]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Owner]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Number]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Number]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[BankName]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[BankName]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Swift]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Swift]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[CardType]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[CardType]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[ExpiryMonth]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[ExpiryMonth]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[ExpiryYear]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[ExpiryYear]&lt;/span&gt;
&lt;span style="color: blue;"&gt;FROM&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[BillingDetails]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;
&lt;span style="color: blue;"&gt;WHERE&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Discriminator]&lt;/span&gt;&amp;nbsp;&lt;span style="color: gray;"&gt;IN&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: red;"&gt;'BankAccount'&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&lt;span style="color: red;"&gt;'CreditCard'&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 15px; padding-bottom: 5px;"&gt;Or the non-polymorphic query for the BankAccount subclass generates this SQL statement: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;table cellSpacing="0" cellPadding="0" width="490"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="padding-left: 20px; background-color: rgb(240, 240, 240);"&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;SELECT&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[BillingDetailId]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[BillingDetailId]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Owner]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Owner]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Number]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Number]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[BankName]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[BankName]&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&amp;nbsp;
&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Swift]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Swift]&lt;/span&gt;
&lt;span style="color: blue;"&gt;FROM&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[BillingDetails]&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;
&lt;span style="color: blue;"&gt;WHERE&lt;/span&gt;&amp;nbsp;&lt;span style="color: teal;"&gt;[Extent1]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Discriminator]&lt;/span&gt;&amp;nbsp;&lt;span style="color: gray;"&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style="color: red;"&gt;'BankAccount'&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td style="padding-top: 10px;"&gt;Note how Code First adds a restriction on the discriminator column and also how it only selects those columns that belong to BankAccount entity. 
&lt;h3&gt;Change Discriminator Column Data Type and Values With Fluent API&lt;a title="FluentAPI" name="FluentAPI"&gt;&lt;/a&gt;&amp;nbsp;&lt;/h3&gt;Sometimes, especially in legacy schemas, you need to override the conventions for the discriminator column so that Code First can work with the schema. The following fluent API code will change the discriminator column name to "BillingDetailType" and the values to "BA" and "CC" for BankAccount and CreditCard respectively: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;OnModelCreating(&lt;span style="color: rgb(43, 145, 175);"&gt;DbModelBuilder&lt;/span&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Map&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;(m&amp;nbsp;=&amp;gt;&amp;nbsp;m.Requires(&lt;span style="color: rgb(163, 21, 21);"&gt;"BillingDetailType"&lt;/span&gt;).HasValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"BA"&lt;/span&gt;))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Map&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;gt;(m&amp;nbsp;=&amp;gt;&amp;nbsp;m.Requires(&lt;span style="color: rgb(163, 21, 21);"&gt;"BillingDetailType"&lt;/span&gt;).HasValue(&lt;span style="color: rgb(163, 21, 21);"&gt;"CC"&lt;/span&gt;));
}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td class="padTop5"&gt;Also, changing the data type of discriminator column is interesting. In the above code, we passed strings to &lt;span class="code"&gt;HasValue&lt;/span&gt; method but this method has been defined to accepts a type of &lt;span class="code"&gt;object&lt;/span&gt;: &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&amp;nbsp;HasValue(&lt;span style="color: blue;"&gt;object&lt;/span&gt;&amp;nbsp;value);&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Therefore, if for example we pass a value of type &lt;span class="code"&gt;int&lt;/span&gt; to it then Code First not only use our desired values (i.e. 1 &amp;amp; 2) in the discriminator column but also changes the column type to be (INT, NOT NULL): &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;pre style="font-family: consolas;"&gt;modelBuilder.Entity&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BillingDetail&lt;/span&gt;&amp;gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Map&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;BankAccount&lt;/span&gt;&amp;gt;(m&amp;nbsp;=&amp;gt;&amp;nbsp;m.Requires(&lt;span style="color: rgb(163, 21, 21);"&gt;"BillingDetailType"&lt;/span&gt;).HasValue(1))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.Map&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;CreditCard&lt;/span&gt;&amp;gt;(m&amp;nbsp;=&amp;gt;&amp;nbsp;m.Requires(&lt;span style="color: rgb(163, 21, 21);"&gt;"BillingDetailType"&lt;/span&gt;).HasValue(2));&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;
&lt;h3&gt;Summary&lt;/h3&gt;In this post we learned about Table per Hierarchy as the default mapping strategy in Code First. The disadvantages of the TPH strategy may be too serious for your design—after all, denormalized schemas can become a major burden in the long run. Your DBA may not like it at all. In the next post, we will learn about Table per Type (TPT) strategy that doesn’t expose you to this problem. &lt;br&gt;
&lt;h3&gt;References&lt;/h3&gt;
&lt;ul&gt;
&lt;li style="padding-top: 0px;"&gt;&lt;a href="http://blogs.msdn.com/b/adonet/" target="_blank" mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/a&gt; &lt;/li&gt;

&lt;li&gt;&lt;a href="http://www.manning.com/bauer2/" target="_blank" mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;style type="text/css"&gt;
        a
        {
            color: #5A99FF;            
        }
        
        a:visited
        {
            color: #5A99FF;
        }
        
        h3 
        {
            border-bottom: 1px solid #DDDDDD;
            color: #333333;
            font-size: 14px;
            font-family: Segoe UI,arial,serif;
            padding-bottom: 0.2em;
            font-weight: normal;            
            line-height: 22px;
            padding-top: 10px;
            /*margin: 33px 0 11px;*/
        }
    
        td         
        {
                line-height: 18px;
        }
        
        .title
        {
            padding-bottom: 5px;
            font-family: Segoe UI;
            font-size: 11pt;
            font-weight: bold;
            padding-top: 15px;
        }
        
        .code, .typeName
        {
            font-family: consolas;
        }
        
        .typeName
        {
            color: #2b91af;
        }
        .myComment
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
        .commentowner11
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
        
        .padTop5
        {
            padding-top: 5px;
        }
        
        .padTop10
        {
            padding-top: 10px;
        }
        p.MsoNormal
        {
            margin-top: 0in;
            margin-right: 0in;
            margin-bottom: 10.0pt;
            margin-left: 0in;
            line-height: 115%;
            font-size: 11.0pt;
            font-family: "Calibri" , "sans-serif";
        }
    &lt;/style&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7666815" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="CTP5" scheme="http://weblogs.asp.net/manavi/archive/tags/CTP5/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Associations in EF Code First CTP5: Part 2 – Shared Primary Key Associations</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx" /><id>http://weblogs.asp.net/manavi/archive/2010/12/19/entity-association-mapping-with-code-first-one-to-one-shared-primary-key-associations.aspx</id><published>2010-12-19T03:09:00Z</published><updated>2010-12-19T03:09:00Z</updated><content type="html">&lt;TABLE style="FONT-FAMILY: arial; FONT-SIZE: 10pt" border=0 cellSpacing=0 cellPadding=0&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;In the previous &lt;A href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx" target=_blank mce_href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx"&gt;blog post&lt;/A&gt; I demonstrated how to map a special kind of one-to-one association—a composition with complex types as the first post in a series about entity association mapping with EF Code First. We argued that the relationships between User and Address are best represented with a complex type mapping and we saw that this is usually the simplest way to represent one-to-one relationships but comes with some limitations. &lt;BR&gt;&lt;BR&gt;In today’s blog post I’m going to discuss how we can address those limitations by changing our mapping strategy. This is particularly useful for scenarios that we want a dedicated table for Address, so that we can map both User and Address as entities. One benefit of this model is the possibility for &lt;I&gt;shared references&lt;/I&gt;— another entity class (let’s say Shipment) can also have a reference to a particular Address instance. If a User has a reference to this instance, as her BillingAddress, the Address instance has to support shared references and needs its own identity. In this case, User and Address classes have a true &lt;I&gt;one-to-one&lt;/I&gt; association. 
&lt;H3&gt;Introducing the Revised Model&lt;/H3&gt;In this revised version, each User &lt;EM&gt;could&lt;/EM&gt; have one BillingAddress (Billing Association). Also Shipment has to be delivered to an address so it &lt;EM&gt;always&lt;/EM&gt; has one Delivery Address (Delivery Association). Here is the class diagram for this domain model (note the multiplicities on association lines): &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-BOTTOM: 15px; PADDING-TOP: 15px" align=center&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post02/ClassDiagram.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;In this model we assumed that the billing address of the user is the same as her delivery address. Now let’s create the association mappings for this domain model. There are several choices, the first being a &lt;I&gt;One-to-One Primary Key Association&lt;/I&gt;. 
&lt;H3&gt;&lt;A title=SharedPrimaryKeyAssociations name=SharedPrimaryKeyAssociations&gt;&lt;/A&gt;Shared Primary Associations&lt;/H3&gt;Also know as &lt;I&gt;One-to-One Primary Key Associations&lt;/I&gt;, means two related tables share the same primary key values. The primary key of one table is also a foreign key of the other. Let’s see how we map the primary key associations with Code First. 
&lt;H3&gt;How to Implement a One-to-One Primary Key Association with Code First&lt;/H3&gt;First, we start with the POCO classes. As you can see, we've defined &lt;I&gt;BillingAddress&lt;/I&gt; as a navigation property on User class and another one on Shipment class named &lt;I&gt;DeliveryAddress&lt;/I&gt;. Both associations are unidirectional since we didn't define related navigation properties on Address class as for User and Shipment. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;FirstName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;LastName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;virtual&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;BillingAddress&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}
 
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;AddressId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;PostalCode&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}
 
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Shipment&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;ShipmentId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;&amp;nbsp;CreatedOn&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;State&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;virtual&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;DeliveryAddress&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}
 
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;&amp;nbsp;:&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbContext&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbSet&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;&amp;nbsp;Users&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbSet&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;gt;&amp;nbsp;Addresses&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbSet&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Shipment&lt;/SPAN&gt;&amp;gt;&amp;nbsp;Shipments&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;!--&lt;div class="title" style="padding-top: 10px;"&gt;
                        Independent Association vs. Foreign Key Association&lt;/div&gt;
                    Associations can fall into two types based on the existence of the foreign key property:
                    
&lt;ul&gt;
                        
&lt;li&gt;&lt;b&gt;Foreign Key Association:&lt;/b&gt; Is when the foreign key for the association is defined
                            as an scalar property along with the navigation property on the dependent end (e.g.
                            User.AddressId).&lt;/li&gt;









                        
&lt;li style="padding-top: 5px"&gt;&lt;b&gt;Independent Association:&lt;/b&gt; Is when the foreign key
                            for the association is not defined on the dependent end. So you need to go the navigation
                            property to access it (e.g. User.Address.AddressId)&lt;/li&gt;









                    &lt;/ul&gt;









                    If you take a closer look at our object model, you'll see that we have &lt;i&gt;independent
                        associations&lt;/i&gt; on both User-Address and Shipment-Address since there is no
                    foreign key property (i.e. an AddressId) defined on User and Shipment classes.--&gt;
&lt;H3 style="PADDING-TOP: 0px"&gt;How Code First Reads This Object Model: One-to-Many&lt;/H3&gt;Code First reads the model and tries to figure out the multiplicity of the associations. Since the associations are unidirectional, Code First takes this as if one Address has many Users and Many Shipments and will create a &lt;I&gt;one-to-many&lt;/I&gt; association for each of them. So, what we were hoping for —a one-to-one association, is not inline with the conventions. 
&lt;H3&gt;How to Change the Multiplicity to One-to-One by Using the Conventions&lt;/H3&gt;One way to turn our associations to be one-to-one is by making them bidirectional. That is, adding a new navigation property to Address class of type User and another one of type Shipment. By doing that we basically signal Code First that we are looking to have one-to-one associations since for example User has an Address and also Address has a User. Based on the conventions, Code First will change the multiplicity to one-to-one and this will solve the problem. 
&lt;H3&gt;Should We Make This Association Bidirectional? &lt;/H3&gt;As always, the decision is up to us and depends on whether we need to navigate through our objects in that direction in the application code. In this case, we’d probably conclude that the bidirectional association doesn’t make much sense. If we call &lt;I&gt;anAddress.User&lt;/I&gt;, we are saying “give me the user who has this address”, not a very reasonable request. So this is not a good option. Instead we'll keep our object model as it is and will resort to fluent API. 
&lt;H3&gt;How to Change the Multiplicity to One-to-One with Fluent API&lt;/H3&gt;The following code is all that is needed to make the associations to be one-to-one. Note how the multiplicities in the UML class diagram (e.g. 1 on User and 0..1 on address) has been translated to the flunet API code by using &lt;I&gt;HasRequired&lt;/I&gt; and &lt;I&gt;HasOptional&lt;/I&gt; methods: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt;&amp;nbsp;OnModelCreating(&lt;SPAN style="COLOR: #2b91af"&gt;ModelBuilder&lt;/SPAN&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithRequired();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Shipment&lt;/SPAN&gt;&amp;gt;().HasRequired(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithOptional();
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Also it worth mentioning that in CTP5, when we are mapping a one-to-one association with fluent API, we don't need to specify the foreign key as we would do when mapping a one-to-many association with &lt;I&gt;HasForeignKey&lt;/I&gt; method. Since EF only supports one-to-one primary key associations it will automatically create the relationship in the database based on the primary keys and we don't need to state the obvious as we did in CTP4. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;Database Schema&lt;/H3&gt;The mapping result for our object model is as follows (note the Identity column): &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 10px"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post02/DbSchema.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 0px"&gt;
&lt;H3&gt;Referential Integrity&lt;/H3&gt;In relational database design the referential integrity rule states that each non-null value of a foreign key must match the value of some primary key. But wait, how does it even applies here? All we have is just three primary keys referencing each other. Who is the primary key and who is the foreign key? The best way to find the answer of this question is to take a look at the properties of the relationships in the database that has been created by Code First: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 15px"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post02/RefIntg1.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 15px"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post02/RefIntg2.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 15px"&gt;As you can see, Code First adds a foreign key constraint which links the primary key of the Addresses table to the primary key of the Users table and adds another foreign key constraint that links the primary key of the Shipments table to the primary key of the Addresses table. The foreign key constraint means that a user has to exist for a particular address but &lt;U&gt;not&lt;/U&gt; the other way around. In other words, the database guarantees that an Addresses row’s primary key references a valid Users primary key and a Shipments row’s primary key references a valid Addresses primary key. 
&lt;H3&gt;How Code First Determines Principal and Dependent?&lt;/H3&gt;Code First has rules to determine the principal and dependent ends of an association. For one-to-many relationships the many end is always the dependent, but it gets a little tricky in one-to-one associations. In one-to-one associations Code First decides based on our object model, and possible data annotations or fluent API that we may have. For example in our case, we wrote this fluent API code to configure User-Address association: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress).WithRequired();&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;This reads as "User entity has an &lt;U&gt;optional&lt;/U&gt; association with one Address object but this association is &lt;U&gt;required&lt;/U&gt; for Address entity." &lt;BR&gt;For Code First this is good enough to make the decision: It marked User as the principal end and Address as the dependent end in the association. Since we have the same fluent API code for the second association between Address and Shipment, it marks Address as the principal end and Shipment as the dependent end in this association as well. &lt;BR&gt;&lt;BR&gt;The &lt;U&gt;referential integrity&lt;/U&gt; that we saw, is the &lt;U&gt;first result&lt;/U&gt; of this Code First's &lt;U&gt;principal/dependent&lt;/U&gt; decision. 
&lt;H3&gt;Second Result of Code First's Principal/Dependent Decision: Database Identity &lt;/H3&gt;If you take a closer look at the above DB schema, you'll notice that only UserId has a regular identifier generator (aka &lt;I&gt;Identity&lt;/I&gt; or &lt;I&gt;Sequence&lt;/I&gt;) and AddressId and ShipmentId does not. This is a very important consequence of the principal/dependent decision for one-to-one associations: &lt;I&gt;the dependent primary key will become non-Identity by default&lt;/I&gt;. This make sense because they share their primary key values and only one of them can be auto generated and we need to take care of providing valid keys for the rest. &lt;/TD&gt;&lt;/TR&gt;&lt;!--
&lt;tr&gt;
&lt;td&gt;
&lt;DIV class=title&gt;Third Result of Code First's Principal/Dependent Decision: Transitive Deletion&lt;/DIV&gt;
Imagine that we want to delete a User object which has an Address. We have to pass this object to
the &lt;span class="code"&gt;Remove()&lt;/span&gt; method on the Users DbSet; 
it’s now in &lt;span class="code"&gt;Deleted&lt;/span&gt; state and will be gone from the database when the &lt;span class="code"&gt;SaveChanges()&lt;/span&gt;is invoked on persistence 
context (we assumed that we have a User with UserId=1 in the database):
&lt;/td&gt;







&lt;/tr&gt;







&lt;tr&gt;
    
&lt;td&gt;
    
        
&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;using&lt;/span&gt;&amp;nbsp;(&lt;span 
            style="color: #2b91af"&gt;EntityMappingContext&lt;/span&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;span 
            style="color: blue"&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;EntityMappingContext&lt;/span&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af"&gt;User&lt;/span&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;context.Users.Find(1);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Remove(user);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();
}&lt;/pre&gt;
    
    &lt;/td&gt;







&lt;/tr&gt;



--&gt;&lt;!--
&lt;tr&gt;
&lt;td&gt;
When we run this code the User and her related address will be gone. But wait, how that is even possible?
If we try to manually delete this User from the database, we’ll get a foreign key constraint violation if an Address holds a reference
to the User. It’s our responsibility to delete the related Address before trying to delete the User.
However, our code ran perfectly fine and deleted the User and her Address. Looking into SQL Profiler will reveal the mystery:

&lt;/td&gt;



&lt;/tr&gt;



--&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;What about Cascade Deletes?&lt;/H3&gt;As we saw, each Address always belongs to one User and each Shipment always delivered to one single Address. We want to make sure that when we delete a User the possible dependent rows on Address and Shipment also get deleted in the database. In fact, this is one of the &lt;A href="http://www.agiledata.org/essays/databaseRefactoringCatalogReferentialIntegrity.html" target=_blank&gt;Referential Integrity Refactorings&lt;/A&gt; which called &lt;A style="FONT-STYLE: italic" href="http://databaserefactoring.com/IntroduceCascadingDelete.html" target=_blank&gt;Introduce Cascading Delete&lt;/A&gt;. The primary reason we would apply "Introduce Cascading Delete" is to preserve the referential integrity of our data by ensuring that related rows are appropriately deleted when a parent row is deleted. By default, Code First does &lt;U&gt;not&lt;/U&gt; enable cascade delete when it creates a relationship in the database. As always we can override this convention with fluent API: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt;&amp;nbsp;OnModelCreating(&lt;SPAN style="COLOR: #2b91af"&gt;ModelBuilder&lt;/SPAN&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithRequired()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WillCascadeOnDelete();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Shipment&lt;/SPAN&gt;&amp;gt;().HasRequired(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddress)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WithOptional()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.WillCascadeOnDelete();
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3 style="PADDING-TOP: 0px"&gt;What If Both Ends are Required? &lt;/H3&gt;We saw that the only reason Code First could figure out principal and dependent in our 1:1 associations was because our fluent API code clearly specified one end as Required and the other as Optional. But what if both endpoints are the same in terms of being required in the association? For example what if in our domain model, User always has one Address and Address always has one User (required on both end)? &lt;!--Or what if Shipment can exists without an Address and Address also can exist without a Shipment (optional on both end)?--&gt;The answer is that ultimately, this scenario need to be configured by fluent API and the interesting point is that fluent API is designed in a way that will &lt;I&gt;force&lt;/I&gt; you to explicitly specify who is dependent and who is principal in such cases that this cannot be inferred by Code First. &lt;BR&gt;&lt;BR&gt;To illustrate the idea, let's see how we can configure mapping for this User-Address association (Required/Required) with fluent API: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;modelBuilder.Entity&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;gt;().HasRequired(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.BillingAddress).WithRequiredDependent();
&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;So we invoke &lt;SPAN class=typeName&gt;WithRequiredDependent()&lt;/SPAN&gt; after &lt;SPAN class=code&gt;HasRequired()&lt;/SPAN&gt; method. To see the reason, we need to take a look at the &lt;SPAN class=code&gt;RequiredNavigationPropertyConfiguration&lt;/SPAN&gt; type which is returned by &lt;SPAN class=code&gt;HasRequired()&lt;/SPAN&gt;: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;RequiredNavigationPropertyConfiguration&lt;/SPAN&gt;&amp;lt;TEntityType,&amp;nbsp;TTargetEntityType&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DependentNavigationPropertyConfiguration&lt;/SPAN&gt;&amp;lt;TEntityType,&amp;nbsp;TTargetEntityType&amp;gt;&amp;nbsp;WithMany();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;CascadableNavigationPropertyConfiguration&lt;/SPAN&gt;&amp;nbsp;WithOptional();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;CascadableNavigationPropertyConfiguration&lt;/SPAN&gt;&amp;nbsp;WithRequiredDependent();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;CascadableNavigationPropertyConfiguration&lt;/SPAN&gt;&amp;nbsp;WithRequiredPrincipal();
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;As you can see, if you want to go another Required after &lt;SPAN class=code&gt;HasRequired()&lt;/SPAN&gt; method, you have to either call &lt;SPAN class=code&gt;WithRequiredDependent()&lt;/SPAN&gt; or &lt;SPAN class=code&gt;WithRequiredPrincipal()&lt;/SPAN&gt; since there is no &lt;SPAN class=code&gt;WithRequired()&lt;/SPAN&gt; method on this &lt;SPAN class=code&gt;RequiredNavigationPropertyConfiguration&lt;/SPAN&gt; class which is returned by &lt;SPAN class=code&gt;HasRequired()&lt;/SPAN&gt; method. &lt;/TD&gt;&lt;/TR&gt;&lt;!--
&lt;tr&gt;
    
&lt;td style="padding-top: 20px;"&gt;
    
    Furthermore, the fluent API code for mapping the Address-Shipment association (Optional/Optional) looks like this:
    &lt;/td&gt;







&lt;/tr&gt;







&lt;tr&gt;
    
&lt;td&gt;
    
&lt;pre style="font-family: consolas"&gt;modelBuilder.Entity&amp;lt;&lt;span style="color: #2b91af"&gt;Shipment&lt;/span&gt;&amp;gt;().HasOptional(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.DeliveryAddress).WithOptionalPrincipal();&amp;nbsp;
&lt;/pre&gt;
    &lt;/td&gt;







&lt;/tr&gt;







&lt;tr&gt;
    
&lt;td&gt;
        Again the same story happens here, we chain &lt;span class="typeName"&gt;WithOptionalPrincipal()&lt;/span&gt; after
    &lt;span class="code"&gt;HasOptional()&lt;/span&gt; method which 
    returns an object of type &lt;span class="code"&gt;OptionalNavigationPropertyConfiguration&lt;/span&gt;. Here is a snapshot of this class:
    &lt;/td&gt;







&lt;/tr&gt;







&lt;tr&gt;
    
&lt;td&gt;
        
        
&lt;pre style="font-family: consolas"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span 
            style="color: blue"&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;OptionalNavigationPropertyConfiguration&lt;/span&gt;&amp;lt;TEntityType,&amp;nbsp;TTargetEntityType&amp;gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;DependentNavigationPropertyConfiguration&lt;/span&gt;&amp;lt;TEntityType,&amp;nbsp;TTargetEntityType&amp;gt;&amp;nbsp;WithMany();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;CascadableNavigationPropertyConfiguration&lt;/span&gt;&amp;nbsp;WithOptionalDependent();&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;CascadableNavigationPropertyConfiguration&lt;/span&gt;&amp;nbsp;WithOptionalPrincipal();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #2b91af"&gt;CascadableNavigationPropertyConfiguration&lt;/span&gt;&amp;nbsp;WithRequired();
}&lt;/pre&gt;
        
    &lt;/td&gt;







&lt;/tr&gt;







&lt;tr&gt;
    
&lt;td&gt;
    Therefore, by design going another Optional after &lt;span class="code"&gt;HasOptional()&lt;/span&gt; method is not possible unless you explicitly clarify
    the principal/dependent situation by 
    either calling &lt;span class="code"&gt;WithOptionalDependent()&lt;/span&gt; or &lt;span class="code"&gt;WithOptionalPrincipal()&lt;/span&gt; since
    there is no &lt;span class="code"&gt;WithOptional()&lt;/span&gt; method on this &lt;span class="code"&gt;OptionalNavigationPropertyConfiguration&lt;/span&gt; class.
    &lt;/td&gt;







&lt;/tr&gt;







--&gt;
&lt;TR&gt;
&lt;TD&gt;Both &lt;SPAN class=code&gt;WithRequired&lt;/SPAN&gt; and &lt;SPAN class=code&gt;WithOptional&lt;/SPAN&gt; methods return a &lt;SPAN class=code&gt;CascadableNavigationPropertyConfiguration&lt;/SPAN&gt; type which has a &lt;SPAN class=typeName&gt;WillCascadeOnDelete()&lt;/SPAN&gt; method. Now if we run the code and check the database, we'll see that cascade delete on both relationships are switched on. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;Working with the Model&lt;/H3&gt;Here is an example for adding a new user along with its billing address. EF is smart enough to use the newly generated UserId for the AddressId as well: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt;&amp;nbsp;(&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;billingAddress&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Street&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Yonge&amp;nbsp;St."&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Toronto"&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;nbsp;morteza&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Morteza"&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Manavi"&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BillingAddress&amp;nbsp;=&amp;nbsp;billingAddress
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Add(morteza);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;The following code is an example of adding a new Address and Shipment for an existing User (assuming that we have a User with UserId=2 in the database): &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt;&amp;nbsp;(&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;deliveryAddress&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AddressId&amp;nbsp;=&amp;nbsp;2,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Street&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Main&amp;nbsp;St."&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;City&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Seattle"&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Shipment&lt;/SPAN&gt;&amp;nbsp;shipment&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Shipment&lt;/SPAN&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShipmentId&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;2,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;State&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Shipped"&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CreatedOn&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DateTime&lt;/SPAN&gt;.Now,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DeliveryAddress&amp;nbsp;=&amp;nbsp;deliveryAddress
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Shipments.Add(shipment);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3 style="PADDING-TOP: 0px"&gt;Limitations of This Mapping&lt;/H3&gt;There are two important limitations to associations mapped as shared primary key: 
&lt;UL&gt;
&lt;LI&gt;&lt;B&gt;Difficulty in saving related objects:&lt;/B&gt; The main difficulty with this approach is ensuring that associated instances are assigned the same primary key value when the objects are saved. For example, when adding a new Address object, it's our responsibility to provide a unique AddressId that is also valid (a User can be found with such a value as UserId.)&lt;/LI&gt;&lt;BR&gt;
&lt;LI&gt;&lt;B&gt;Multiple addresses for User is not possible:&lt;/B&gt; With this mapping we cannot have more than one Address for User. At the beginning of this post, when we introduce our model, we assumed that the user has the same address for billing and delivery. But what if that's not the case? What if we also want to add a Home address to User for the deliveries? In the current setup, each row in the User table has a corresponding row in the Address table. Two addresses would require an additional address table, and this mapping style therefore wouldn’t be adequate.&lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;Summary&lt;/H3&gt;In this post we learned about one-to-one associations which shared primary key is just one way to implement it. Shared primary key associations aren’t uncommon but are relatively rare. In many schemas, a one-to-one association is represented with a foreign key field and a unique constraint. In the next posts we will revisit the same domain model and will learn about other ways to map one-to-one associations that does not have the limitations of the shared primary key association mapping. &lt;BR&gt;
&lt;H3 style="PADDING-BOTTOM: 0px"&gt;References&lt;/H3&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/b/adonet/" target=_blank mce_href="http://blogs.msdn.com/b/adonet/"&gt;ADO.NET team blog&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.manning.com/bauer2/" target=_blank mce_href="http://www.manning.com/bauer2/"&gt;Java Persistence with Hibernate book&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;STYLE type=text/css&gt;    
    a
    {            
        color: #5A99FF;
    }
    
    a:visited 
    {
        color: #5A99FF;
    }
    
    h3 
    {
        border-bottom: 1px solid #DDDDDD;
        color: #333333;
        font-size: 14px;
        font-family: Segoe UI,arial,serif;
        padding-bottom: 0.2em;
        font-weight: normal;            
        line-height: 22px;
        padding-top: 10px;
        /*margin: 33px 0 11px;*/
    }
    
    td         
    {
            line-height: 18px;
    }

    li
    {
        padding-top:0px;
    }
    
    .code, .typeName
    {
        font-family: consolas;           
    }
    
    .typeName
    {
        color: #2b91af;
    }

.myComment
        {
            border-bottom: #cdcdcd 1px solid;
            border-left: #cdcdcd 1px solid;
            line-height: 18px;
            background-color: #f2f2f2;
            padding-top: 9px;
            padding-left: 9px;
            padding-bottom: 9px;
            padding-right: 9px;
            border-top: #cdcdcd 1px solid;
            border-right: #cdcdcd 1px solid;
        }
    
&lt;/STYLE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7664002" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="CTP5" scheme="http://weblogs.asp.net/manavi/archive/tags/CTP5/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>Associations in EF Code First CTP5: Part 1 – Complex Types</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx" /><id>http://weblogs.asp.net/manavi/archive/2010/12/11/entity-association-mapping-with-code-first-part-1-one-to-one-associations.aspx</id><published>2010-12-11T08:21:00Z</published><updated>2010-12-11T08:21:00Z</updated><content type="html">&lt;TABLE style="FONT-FAMILY: arial; FONT-SIZE: 10pt" border=0 cellSpacing=0 cellPadding=0&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;Last week the CTP5 build of the new Entity Framework Code First has been &lt;A href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-released.aspx" target=_blank&gt;released&lt;/A&gt; by data team at Microsoft. Entity Framework Code-First provides a pretty powerful code-centric way to work with the databases. When it comes to associations, it brings ultimate flexibility. I’m a big fan of the EF Code First approach and I am planning to explain association mapping with code first in a series of blog posts and this one is dedicated to Complex Types. 
&lt;H3&gt;A Note For Those Who are New to Entity Framework and Code-First&lt;/H3&gt;If you choose to learn EF you've chosen well. If you choose to learn EF with Code First you've done even better. To get started, you can find a great walkthrough by Scott Guthrie &lt;A href="http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx" target=_blank mce_href="http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx"&gt;here&lt;/A&gt; and another one by ADO.NET team &lt;A href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx" target=_blank mce_href="http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx"&gt;here&lt;/A&gt;. In this post, I assume you already setup your machine to do Code First development and also that you are familiar with Code First fundamentals and basic concepts. &lt;BR&gt;&lt;BR&gt;In order to build a solid foundation for our discussion, we will start by learning about some of the core concepts around the relationship mapping. &amp;nbsp; 
&lt;H3&gt;What is Mapping?&lt;/H3&gt;&lt;I&gt;Mapping&lt;/I&gt; is the act of determining how objects and their relationships are persisted in permanent data storage, in our case, relational databases. 
&lt;H3&gt;What is Relationship Mapping?&lt;/H3&gt;A mapping that describes how to persist a relationship (association, aggregation, or composition) between two or more objects. 
&lt;H3&gt;Types of Relationships&lt;/H3&gt;There are two categories of object relationships that we need to be concerned with when mapping associations. The first category is based on &lt;I&gt;multiplicity&lt;/I&gt; and it includes three types: 
&lt;UL&gt;
&lt;LI style="PADDING-TOP: 0px"&gt;&lt;B&gt;One-to-one relationships:&lt;/B&gt; This is a relationship where the maximums of each of its multiplicities is one. &lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;One-to-many relationships:&lt;/B&gt; Also known as a many-to-one relationship, this occurs when the maximum of one multiplicity is one and the other is greater than one. &lt;/LI&gt;
&lt;LI style="PADDING-BOTTOM: 5px"&gt;&lt;B&gt;Many-to-many relationships:&lt;/B&gt; This is a relationship where the maximum of both multiplicities is greater than one. &lt;/LI&gt;&lt;/UL&gt;The second category is based on &lt;EM&gt;directionality&lt;/EM&gt; and it contains two types: 
&lt;UL&gt;
&lt;LI style="PADDING-TOP: 0px"&gt;&lt;B&gt;Uni-directional relationships: &lt;/B&gt;when an object knows about the object(s) it is related to but the other object(s) do not know of the original object. To put this in EF terminology, when a navigation property exists only on one of the association ends and not on the both. &lt;/LI&gt;
&lt;LI style="PADDING-TOP: 10px"&gt;&lt;B&gt;Bi-directional relationships:&lt;/B&gt; When the objects on both end of the relationship know of each other (i.e. a navigation property defined on both ends).&lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;How Object Relationships are Implemented in POCO Object Models?&lt;/H3&gt;When the multiplicity is one (e.g. 0..1 or 1) the relationship is implemented by defining a &lt;EM&gt;navigation property&lt;/EM&gt; that reference the&lt;/SPAN&gt;&lt;/SPAN&gt; other object (e.g. an Address property on User class). When the multiplicity is many (e.g. 0..*, 1..*) the relationship is implemented via an &lt;A style="FONT-STYLE: italic" href="http://msdn.microsoft.com/en-us/library/system.collections.icollection.aspx" target=_blank&gt;ICollection&lt;/A&gt; of the type of other object. 
&lt;H3&gt;How Relational Database Relationships are Implemented? &lt;/H3&gt;Relationships in relational databases are maintained through the use of &lt;EM&gt;Foreign Keys&lt;/EM&gt;. A foreign key is a data attribute(s) that appears in one table and must be the primary key or other candidate key in another table. With a one-to-one relationship the foreign key needs to be implemented by one of the tables. To implement a one-to-many relationship we implement a foreign key from the “one table” to the “many table”. We could also choose to implement a one-to-many relationship via an &lt;EM&gt;associative table&lt;/EM&gt; (aka &lt;EM&gt;Join table&lt;/EM&gt;), effectively making it a many-to-many relationship. 
&lt;H3&gt;Introducing the Model&lt;/H3&gt;Now, let's review the model that we are going to use in order to implement Complex Type with Code First. It's a simple object model which consist of two classes: &lt;EM&gt;User&lt;/EM&gt; and &lt;EM&gt;Address.&lt;/EM&gt; Each user &lt;EM&gt;could&lt;/EM&gt; have one billing address. The Address information of a User is modeled as a separate class as you can see in the UML model below: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post01/Composition.jpg" mce_src="http://weblogs.asp.net/blogs/manavi/Post01/Composition.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;In object-modeling terms, this association is a kind of &lt;EM&gt;aggregation&lt;/EM&gt;—a &lt;EM&gt;part-of&lt;/EM&gt; relationship. Aggregation is a strong form of association; it has some additional semantics with regard to the lifecycle of objects. In this case, we have an even stronger form, &lt;EM&gt;composition&lt;/EM&gt;, where the lifecycle of the part is fully dependent upon the lifecycle of the whole. 
&lt;H3&gt;Fine-grained Domain Models &lt;/H3&gt;The motivation behind this design was to achieve &lt;I&gt;Fine-grained domain models.&lt;/I&gt; In crude terms, fine-grained means “more classes than tables”. For example, a user may have both a billing address and a home address. In the database, you may have a single User table with the columns BillingStreet, BillingCity, and BillingPostalCode along with HomeStreet, HomeCity, and HomePostalCode. There are good reasons to use this somewhat &lt;I&gt;denormalized&lt;/I&gt; relational model (performance, for one). In our object model, we can use the same approach, representing the two addresses as six string-valued properties of the User class. But it’s much better to model this using an Address class, where User has the BillingAddress and HomeAddress properties. This object model achieves improved &lt;I&gt;cohesion&lt;/I&gt; and &lt;I&gt;greater code reuse&lt;/I&gt; and is more understandable. 
&lt;H3&gt;Complex Types: Splitting a Table Across Multiple Types&lt;/H3&gt;Back to our model, there is no difference between this composition and other weaker styles of association when it comes to the actual C# implementation. But in the context of ORM, there is a big difference: A composed class is often a candidate &lt;EM&gt;Complex Type&lt;/EM&gt;. But C# has no concept of composition—a class or property can’t be marked as a composition. The only difference is the object identifier: a complex type has &lt;EM&gt;no&lt;/EM&gt; individual identity (i.e. no AddressId defined on Address class) which make sense because when it comes to the database everything is going to be saved into one single table.&lt;BR&gt;
&lt;H3&gt;How to implement a Complex Type with Code First &lt;/H3&gt;Code First has a concept of &lt;EM&gt;Complex Type Discovery&lt;/EM&gt; that works based on a set of &lt;A style="FONT-STYLE: italic" href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx" target=_blank mce_href="http://blogs.msdn.com/b/efdesign/archive/2010/06/01/conventions-for-code-first.aspx"&gt;Conventions&lt;/A&gt;. The convention is that if Code First discovers a class where &lt;SPAN style="TEXT-DECORATION: underline"&gt;a primary key cannot be inferred&lt;/SPAN&gt;, and &lt;SPAN style="TEXT-DECORATION: underline"&gt;no primary key is registered through Data Annotations or the fluent API&lt;/SPAN&gt;, then the type will be &lt;I&gt;automatically&lt;/I&gt; registered as a complex type. Complex type detection also requires that the type does not have properties that reference entity types (i.e. all the properties must be scalar types) and is not referenced from a collection property on another type. Here is the implementation: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: "&gt;&lt;FONT face=Consolas&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#2b91af&gt;User&lt;/FONT&gt;&lt;/SPAN&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;int&lt;/FONT&gt;&lt;/SPAN&gt; UserId { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/SPAN&gt; FirstName { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/SPAN&gt; LastName { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/SPAN&gt; Username { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#2b91af&gt;Address&lt;/FONT&gt;&lt;/SPAN&gt; Address { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Consolas&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#2b91af&gt;Address&lt;/FONT&gt;&lt;/SPAN&gt; &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/SPAN&gt; Street { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/SPAN&gt; City { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;&lt;/SPAN&gt; PostalCode { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Consolas&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#0000ff&gt;class&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#2b91af&gt;EntityMappingContext&lt;/FONT&gt;&lt;/SPAN&gt; : &lt;SPAN&gt;&lt;FONT color=#2b91af&gt;DbContext&lt;/FONT&gt;&lt;/SPAN&gt; &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;&lt;FONT color=#2b91af&gt;DbSet&lt;/FONT&gt;&lt;/SPAN&gt;&amp;lt;&lt;SPAN&gt;&lt;FONT color=#2b91af&gt;User&lt;/FONT&gt;&lt;/SPAN&gt;&amp;gt; Users { &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;get&lt;/FONT&gt;&lt;/SPAN&gt;; &lt;SPAN&gt;&lt;FONT color=#0000ff&gt;set&lt;/FONT&gt;&lt;/SPAN&gt;; }&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;}&lt;/FONT&gt;&lt;/FONT&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;BR&gt;With code first, this is &lt;SPAN style="TEXT-DECORATION: underline"&gt;all of the code&lt;/SPAN&gt; we need to write to create a complex type, we do not need to configure any additional database schema mapping information through Data Annotations or the fluent API. 
&lt;H3&gt;Database Schema&lt;/H3&gt;The mapping result for this object model is as follows: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 20px"&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post01/DbSchema.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 5px"&gt;
&lt;H3&gt;Complex Types are Required&lt;/H3&gt;As a limitation of EF in general, complex types are always &lt;I&gt;considered&lt;/I&gt; required. To see this limitation in action, let's try to add a record to our database: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt;&amp;nbsp;(&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FirstName&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Morteza"&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LastName&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"Manavi"&lt;/SPAN&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Username&amp;nbsp;&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: #a31515"&gt;"mmanavi"&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.Users.Add(user);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;context.SaveChanges();&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Surprisingly, this code throws a &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.updateexception.aspx" target=_blank;&gt;System.Data.UpdateException&lt;/A&gt; at runtime with this message: &lt;BR&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 8px"&gt;
&lt;DIV style="PADDING-BOTTOM: 5px; BACKGROUND-COLOR: #f0f0f0; PADDING-LEFT: 5px; WIDTH: 328px; PADDING-TOP: 5px"&gt;&lt;I&gt;Null value for non-nullable member. Member: 'Address'.&lt;/I&gt; &lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 10px"&gt;If we initialize the address object, the exception would go away and user will be successfully saved into database: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;IMG src="http://weblogs.asp.net/blogs/manavi/Post01/SaveUser.jpg"&gt; &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;When we read back the inserted record from the database, EF will return an Address object with all the properties (Street, City and PostalCode) have null values. This means that if you store a complex type object with all null property values, EF returns a initialized complex type when the owning entity is retrieved from the database. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;Explicitly Register a Type as Complex&lt;/H3&gt;You saw that in our model, we did not use any data annotation or fluent API code to designate the Address as a complex type, yet Code First perfectly detects it as a complex type based on Complex Type Discovery concept. But what if our domain model requires a new property called Id on Address class? This new Id property is just a scalar non-primary key property that represents let's say another piece of information about address. In this case, Code First actually &lt;I&gt;can&lt;/I&gt; infer a key and therefore marks Address as an entity that has its own mapping table unless we specify otherwise. This is where explicit complex type registration comes into play. CTP5 defined a new attribute in &lt;SPAN class=code&gt;System.ComponentModel.DataAnnotations&lt;/SPAN&gt; namespace called &lt;SPAN class=typeName&gt;ComplexTypeAttribute&lt;/SPAN&gt;. All we need to do is to use this attribute on our Address class: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;ComplexType&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;Id&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;PostalCode&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;This will cause Address to remain as a complex type in our model. As always, we can do the same with fluent API. In CTP5 a new generic method has been added to &lt;SPAN class=code&gt;ModelBuilder&lt;/SPAN&gt; class which is called &lt;SPAN class=code&gt;ComplexType&lt;/SPAN&gt; and has the following signature (when working with fluent API, we don't really care about the method's return values): &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;virtual&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;ComplexTypeConfiguration&lt;/SPAN&gt;&amp;lt;TComplexType&amp;gt;&amp;nbsp;ComplexType&amp;lt;TComplexType&amp;gt;()&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt;&amp;nbsp;TComplexType&amp;nbsp;:&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD style="PADDING-TOP: 10px"&gt;Here is how we can register our Address type as complex in fluent API: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;protected&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt;&amp;nbsp;OnModelCreating(&lt;SPAN style="COLOR: #2b91af"&gt;ModelBuilder&lt;/SPAN&gt;&amp;nbsp;modelBuilder)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;modelBuilder.ComplexType&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;gt;();
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3 style="PADDING-TOP: 0px"&gt;Best Practices When Working with Complex Types &lt;/H3&gt;
&lt;UL&gt;
&lt;LI style="PADDING-TOP: 0px"&gt;&lt;B&gt;Always initialize the complex type:&lt;/B&gt; Because of the problem we just saw, I recommended to always initialize the complex type inside its owning entity's constructor. &lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;Add a read only property to the complex type for null value checking:&lt;/B&gt; Defining a non-persistent read only property like &lt;I&gt;HasValue&lt;/I&gt; will help to test for null values. &lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;Consider always using ComplexType attribute:&lt;/B&gt; Even if your class is automatically detected as a complex type by Code First, I still recommend to mark it with &lt;SPAN class=code&gt;[ComplexType]&lt;/SPAN&gt; attribute. Not only that helps your object model to be more readable but also ensures that your complex types will stay as complex type as your model evolves in your project. Furthermore, there is a bug in CTP5 and that is if you put &lt;SPAN class=code&gt;Required&lt;/SPAN&gt; attribute (a data annotation that Code First supports for validation) on any of the complex type's properties (e.g. PostalCode) then Code First will stop thinking that it is a complex type and will throw a ModelValidationException. The workaround for this bug is to explicitly mark Address with ComplexType attribute. Hence, it will be beneficial in such cases as well. &lt;/LI&gt;&lt;/UL&gt;Therefore, our final object model will be: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;User&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;User()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Address&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;&amp;nbsp;UserId&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;FirstName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;LastName&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;Username&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;Address&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
}
 
[&lt;SPAN style="COLOR: #2b91af"&gt;ComplexType&lt;/SPAN&gt;]
&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;Street&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;City&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;nbsp;PostalCode&amp;nbsp;{&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt;&amp;nbsp;HasValue&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt;&amp;nbsp;(Street&amp;nbsp;!=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;&amp;nbsp;||&amp;nbsp;PostalCode&amp;nbsp;!=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;&amp;nbsp;||&amp;nbsp;City&amp;nbsp;!=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;The interesting point is that we do not have to explicitly exclude &lt;I&gt;HasValue&lt;/I&gt; property from the mapping. Since this property does not have a setter, EF Code First will be ignoring it based on a convention which makes sense since a read only property is most probably represents a computed value and does not need to be persist in the database. &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3&gt;Complex Types and the New Change Tracking API &lt;/H3&gt;EF Code First CTP5 exposes a new set of change tracking information that enables us to access Original, Current &amp;amp; Stored values, and State (e.g. Added, Unchanged, Modified, Deleted) of our entities. The &lt;I&gt;Original Values&lt;/I&gt; are the values the entity had when it was queried from the database. The &lt;I&gt;Current Values&lt;/I&gt; are the values the entity has now. This feature also fully supports complex types: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt;&amp;nbsp;(&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt;&amp;nbsp;context&amp;nbsp;=&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;EntityMappingContext&lt;/SPAN&gt;())
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;var&lt;/SPAN&gt;&amp;nbsp;user&amp;nbsp;=&amp;nbsp;context.Users.Find(1);
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;originalValues&amp;nbsp;=&amp;nbsp;context.Entry(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ComplexProperty(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.OriginalValue;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;Address&lt;/SPAN&gt;&amp;nbsp;currentValues&amp;nbsp;=&amp;nbsp;context.Entry(user)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.ComplexProperty(u&amp;nbsp;=&amp;gt;&amp;nbsp;u.Address)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.CurrentValue;
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;The entry point for accessing the new change tracking API is DbContext's &lt;I&gt;Entry&lt;/I&gt; method which returns an object of type &lt;I&gt;DbEntityEntry&lt;/I&gt;. DbEntityEntry contains a &lt;I&gt;ComplexProperty&lt;/I&gt; method that returns a &lt;I&gt;DbComplexPropertyEntry&lt;/I&gt; object where we can access the original and current values: &lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;&lt;PRE style="FONT-FAMILY: consolas"&gt;&lt;SPAN style="COLOR: blue"&gt;namespace&lt;/SPAN&gt;&amp;nbsp;System.Data.Entity.Infrastructure
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbEntityEntry&lt;/SPAN&gt;&amp;lt;TEntity&amp;gt;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt;&amp;nbsp;TEntity&amp;nbsp;:&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN style="COLOR: #2b91af"&gt;DbComplexPropertyEntry&lt;/SPAN&gt;&amp;lt;TEntity,&amp;nbsp;TComplexProperty&amp;gt;&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ComplexProperty&amp;lt;TComplexProperty&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TEntity,&amp;nbsp;TComplexProperty&amp;gt;&amp;gt;&amp;nbsp;property);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/PRE&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;
&lt;H3 style="PADDING-TOP: 0px"&gt;Limitations of This Mapping&lt;/H3&gt;There are two important limitations to classes mapped as Complex Types: 
&lt;UL&gt;
&lt;LI style="PADDING-TOP: 0px"&gt;&lt;B&gt;Shared references is not possible:&lt;/B&gt; The Address Complex Type doesn’t have its own database identity (primary key) and so can’t be referred to by any object other than the containing instance of User (e.g. a Shipping class that also needs to reference the same User Address). &lt;/LI&gt;
&lt;LI&gt;&lt;B&gt;No elegant way to represent a null reference:&lt;/B&gt; As we saw there is no elegant way to represent a null reference to an Address. When reading from database, EF Code First &lt;I&gt;always&lt;/I&gt; initialize Address object even if values in all mapped columns of the complex type are null. &lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;Summary&lt;/H3&gt;In this post we learned about fine-grained domain models which complex type is just one example of it. Fine-grained is fully supported by EF Code First and is known as the most important requirement for a rich domain model. Complex type is usually the simplest way to represent one-to-one relationships and because the lifecycle is almost always dependent in such a case, it’s either an aggregation or a composition in UML. In the next posts we will revisit the same domain model and will learn about other ways to map a one-to-one association that does not have the limitations of the complex types. &lt;BR&gt;
&lt;H3&gt;References&lt;/H3&gt;
&lt;UL&gt;
&lt;LI style="PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px"&gt;&lt;A href="http://blogs.msdn.com/b/adonet/" target=_blank&gt;ADO.NET team blog&lt;/A&gt; &lt;/LI&gt;
&lt;LI style="PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px"&gt;&lt;A href="http://www.agiledata.org/essays/mappingObjects.html" target=_blank&gt;Mapping Objects to Relational Databases&lt;/A&gt; &lt;/LI&gt;
&lt;LI style="PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px"&gt;&lt;A href="http://www.manning.com/bauer2/" target=_blank&gt;Java Persistence with Hibernate book&lt;/A&gt; &lt;/LI&gt;&lt;/UL&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;STYLE type=text/css&gt;    
    a
    {            
        color: #5A99FF;
    }
    
    a:visited 
    {
        color: #5A99FF;
    }
    
    h3 
    {
        border-bottom: 1px solid #DDDDDD;
        color: #333333;
        font-size: 14px;
        font-family: Segoe UI,arial,serif;
        padding-bottom: 0.2em;
        font-weight: normal;            
        line-height: 22px;
        padding-top: 10px;
        /*margin: 33px 0 11px;*/
    }
    
    td         
    {
            line-height: 18px;
    }

    li
    {
        padding-top:10px;
    }
    
    .code, .typeName
    {
        font-family: consolas;           
    }
    
    .typeName
    {
        color: #2b91af;
    }
    
&lt;/STYLE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7659835" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author><category term=".NET" scheme="http://weblogs.asp.net/manavi/archive/tags/.NET/default.aspx" /><category term="C#" scheme="http://weblogs.asp.net/manavi/archive/tags/C_2300_/default.aspx" /><category term="Code First" scheme="http://weblogs.asp.net/manavi/archive/tags/Code+First/default.aspx" /><category term="CTP5" scheme="http://weblogs.asp.net/manavi/archive/tags/CTP5/default.aspx" /><category term="Entity Framework" scheme="http://weblogs.asp.net/manavi/archive/tags/Entity+Framework/default.aspx" /></entry><entry><title>My First Blog Post</title><link rel="alternate" type="text/html" href="http://weblogs.asp.net/manavi/archive/2010/12/08/my-first-blog-post.aspx" /><id>http://weblogs.asp.net/manavi/archive/2010/12/08/my-first-blog-post.aspx</id><published>2010-12-08T03:25:00Z</published><updated>2010-12-08T03:25:00Z</updated><content type="html">&lt;P style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Finally, here I am, starting my weblog. &lt;BR&gt;&lt;BR&gt;My name is Morteza Manavi. I am going to blog here mostly about technologies that I am passionate about. They are Core C#, Entity Framework, WCF and ASP.Net MVC as well as software design and architecture. &lt;BR&gt;&lt;BR&gt;A special thanks to &lt;A style="COLOR: blue; TEXT-DECORATION: none" href="http://msjoe.com/" target=blank mce_href="http://msjoe.com"&gt;Joe Stagner&lt;/A&gt;, who was kind enough to help me get this blog. &lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7658006" width="1" height="1"&gt;</content><author><name>mortezam</name><uri>http://weblogs.asp.net/members/mortezam.aspx</uri></author></entry></feed>