<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>.net ready !!! : LINQ</title><link>http://blogs.msdn.com/maximelamure/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Basis of LINQ to SQL: Entity Inheritance</title><link>http://blogs.msdn.com/maximelamure/archive/2008/06/02/basis-of-linq-to-sql-entity-inheritance.aspx</link><pubDate>Tue, 03 Jun 2008 01:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8570390</guid><dc:creator>Maxime LAMURE</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/maximelamure/comments/8570390.aspx</comments><wfw:commentRss>http://blogs.msdn.com/maximelamure/commentrss.aspx?PostID=8570390</wfw:commentRss><wfw:comment>http://blogs.msdn.com/maximelamure/rsscomments.aspx?PostID=8570390</wfw:comment><description>&lt;P&gt;The goal of this post is to illustrate the inheritance concept in LINQ to SQL. 
&lt;P&gt;We use inheritance when we want to map a set of classes derived from the same base class with the same relational table. 
&lt;P&gt;Contact will be our Base Class: 
&lt;P&gt;[&lt;FONT color=#00b0b0&gt;Table&lt;/FONT&gt;(Name = "&lt;FONT color=#800000&gt;Contact&lt;/FONT&gt;")]&lt;BR&gt;[&lt;FONT color=#00b0b0&gt;InheritanceMapping&lt;/FONT&gt;(Code = "&lt;FONT color=#800000&gt;Employee&lt;/FONT&gt;", Type = typeof (&lt;FONT color=#00b0b0&gt;EmployeeContact&lt;/FONT&gt;), IsDefault = true)]&lt;BR&gt;[&lt;FONT color=#00b0b0&gt;InheritanceMapping&lt;/FONT&gt;(Code = "&lt;FONT color=#800000&gt;Company&lt;/FONT&gt;", Type = typeof(&lt;FONT color=#00b0b0&gt;CompanyContact&lt;/FONT&gt;))]&lt;/P&gt;
&lt;P&gt;public class &lt;FONT color=#00b0b0&gt;Contact&lt;/FONT&gt; &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#00b0b0&gt;Column&lt;/FONT&gt;(IsDbGenerated=true,IsPrimaryKey=true)] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Guid Id; 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#00b0b0&gt;Column&lt;/FONT&gt;] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string Name; 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#00b0b0&gt;Column&lt;/FONT&gt;(IsDiscriminator = true)] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string EntityType; &lt;BR&gt;} 
&lt;P&gt;The InheritanceMapping attribute specify the corresponding derived class which will be identified by a special discriminator column. The Code parameter défines the value and the Type parameter défines the corresponding derived type. We have to had a field to store the discriminator value (EntityType in our sample). This field is mapped with a column which will have the value of the corresponing type. 
&lt;P&gt;CompanyContact and EmployeeContact derive from Contact 
&lt;P&gt;public class &lt;FONT color=#00b0b0&gt;CompanyContact&lt;/FONT&gt;: &lt;FONT color=#00b0b0&gt;Contact&lt;/FONT&gt; &lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#00b0b0&gt;Column&lt;/FONT&gt;] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string SubName; &lt;BR&gt;} &lt;/P&gt;
&lt;P&gt;public class &lt;FONT color=#00b0b0&gt;EmployeeContact&lt;/FONT&gt;:&lt;FONT color=#00b0b0&gt;Contact&lt;/FONT&gt; &lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;FONT color=#00b0b0&gt;Column&lt;/FONT&gt;] &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public string WebSite; &lt;BR&gt;} 
&lt;P&gt;Those derived classes don’t need to have the table attribute. Because they derived from Contact class, they are mapped with same table than Contact. 
&lt;P&gt;The following class represents our DataContext: 
&lt;P&gt;public class &lt;FONT color=#00b0b0&gt;MyDataContext&lt;/FONT&gt; : &lt;FONT color=#00b0b0&gt;DataContext&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Table&amp;lt;Contact&amp;gt; Contacts;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public MyDataContext(string connection) : base(connection)&lt;BR&gt;} 
&lt;P class=MsoNormal mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;This script creates the correponding DataBase with two records: &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/image_2.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=326 alt=image src="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/image_thumb.png" width=758 border=0 mce_src="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/image_thumb.png"&gt;&lt;/A&gt; 
&lt;P&gt;We can check the result in the SQL Server : 
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/clip_image002%5B4%5D.jpg" mce_href="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/clip_image002%5B4%5D.jpg"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=56 alt=clip_image002[4] src="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/clip_image002%5B4%5D_thumb.jpg" width=408 border=0 mce_src="http://blogs.msdn.com/blogfiles/maximelamure/WindowsLiveWriter/BasisofLINQtoSQLEntityInheritance_38/clip_image002%5B4%5D_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8570390" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/maximelamure/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>