title: LINQ to SQL - mapping exception when using abstract base classes
body: <p>Problem: I would like to share code between multiple assemblies.  This shared code will need to work with LINQ to SQL-mapped classes.</p>
  
  <p>I've encountered the same issue found <a href="https://stackoverflow.com/questions/156113/linqtosql-and-abstract-base-classes">here</a>, but I've also found a work-around that I find troubling (I'm not going so far as to say "bug").</p>
  
  <p><strong>All the following code can be downloaded in <a href="http://www.filehosting.org/file/details/40226/TestLinq2Sql.zip" rel="nofollow noreferrer">this solution</a>.</strong></p>
  
  <p>Given this table:</p>
  
  <pre><code>create table Users
  (
        Id int identity(1,1) not null constraint PK_Users primary key
      , Name nvarchar(40) not null
      , Email nvarchar(100) not null
  )
  </code></pre>
  
  <p>and this DBML mapping:</p>
  
  <pre><code>&lt;Table Name="dbo.Users" Member="Users"&gt;
    &lt;Type Name="User"&gt;
      &lt;Column Name="Id" Modifier="Override" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" /&gt;
      &lt;Column Name="Name" Modifier="Override" Type="System.String" DbType="NVarChar(40) NOT NULL" CanBeNull="false" /&gt;
      &lt;Column Name="Email" Modifier="Override" Type="System.String" DbType="NVarChar(100) NOT NULL" CanBeNull="false" /&gt;
    &lt;/Type&gt;
  &lt;/Table&gt;
  </code></pre>
  
  <p>I've created the following base classes in one assembly "Shared":</p>
  
  <pre><code>namespace TestLinq2Sql.Shared
  {
      public abstract class UserBase
      {
          public abstract int Id { get; set; }
          public abstract string Name { get; set; }
          public abstract string Email { get; set; }
      }
  
      public abstract class UserBase&lt;TUser&gt; : UserBase where TUser : UserBase
      {
          public static TUser FindByName_Broken(DataContext db, string name)
          {
              return db.GetTable&lt;TUser&gt;().FirstOrDefault(u =&gt; u.Name == name);
          }
  
          public static TUser FindByName_Works(DataContext db, string name)
          {
              return db.GetTable&lt;TUser&gt;().FirstOrDefault(u =&gt; u.Name == name &amp;&amp; 1 == 1);
          }
  
          public static TUser FindByNameEmail_Works(DataContext db, string name, string email)
          {
              return db.GetTable&lt;TUser&gt;().FirstOrDefault(u =&gt; u.Name == name || u.Email == email);
          }
      }
  }
  </code></pre>
  
  <p>These classes are referenced in another assembly "Main", like so:</p>
  
  <pre><code>namespace TestLinq2Sql
  {
      partial class User : TestLinq2Sql.Shared.UserBase&lt;User&gt;
      {
  
      }
  }
  </code></pre>
  
  <p>The DBML file is located in the "Main" assembly, as well.</p>
  
  <p>When calling <code>User.FindByName_Broken(db, "test")</code>, an exception is thrown:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: Class member UserBase.Name is unmapped.</p>
  </blockquote>
  
  <p>However, the other two base static methods work. </p>
  
  <p>Furthermore, the SQL generated by calling <code>User.FindByName_Works(db, "test")</code> is what we were hoping for in the broken call:</p>
  
  <pre><code>SELECT TOP (1) [t0].[Id], [t0].[Name], [t0].[Email]
  FROM [dbo].[Users] AS [t0]
  WHERE [t0].[Name] = @p0
  -- @p0: Input NVarChar (Size = 4; Prec = 0; Scale = 0) [test]
  </code></pre>
  
  <p>While I am willing to use this <code>1 == 1</code> "hack" for single predicate queries, is there a better way of sharing LINQ to SQL-aware code in a base/shared/core assembly?</p>