title: Generic method is picking up type of base class
body: <p>I have the following classes (trimmed to only show the basic structure):</p>
  
  <pre><code>public abstract class BaseModel {
      public bool PersistChanges() {
          // Context is of type "ObjectContext"
          DatabaseHelper.Context.SafelyPersistChanges(this);
      }
  }
  
  public static class ObjectContextExtensions {
      public static bool SafelyPersistChanges&lt;T&gt;(this ObjectContext oc, T obj) {
          // Persist the object using a transaction
      }
  }
  
  [Persistent("LEADS")]
  public class Lead : BaseModel {
      // Extra properties
  }
  
  public class LeadsController : Controller {
      public ActionResult Save(Lead lead) {
          lead.PersistChanges()
      }
  }
  </code></pre>
  
  <p>My <strong>Lead</strong> class derives from <strong>BaseModel</strong>, which contains a method to persist the object's changes to the database using a transaction. I implemented the transactional persist with an extension method. The problem is that by passing <strong>this</strong> to <strong>SafelyPersistChanges</strong> in my <strong>BaseModel</strong> class, the generic T on the extension method is set to <strong>BaseModel</strong>. However, since <strong>BaseModel</strong> isn't marked as a persistent object (which it cannot be), the ORM framework throws an exception.</p>
  
  <p>Example:</p>
  
  <pre><code>Lead lead = LeadRepository.FindByNumber(2);
  lead.SalesmanNumber = 4;
  // Calls "ObjectContextExtensions.SafelyPersistChanges&lt;BaseModel&gt;(BaseModel obj)"
  // instead of "ObjectContextExtensions.SafelyPersistChanges&lt;Lead&gt;(Lead obj)"
  lead.PersistChanges();
  </code></pre>
  
  <p>The above block raises the following exception:</p>
  
  <blockquote>
    <p>Cannot create mapping for type 'SalesWeb.Data.BaseModel' without persistent attribute.</p>
  </blockquote>
  
  <p>Any ideas?</p>