title: How to query EntityFramework using generic property type?
body: <p>When I call <code>DbSet.FirstOrDefault()</code> by passing a predicate that compares generic type <code>TId</code>, I get the following exception:</p>
  
  <blockquote>
    <p>unable to create a constant value of type 'system.object'. only
    primitive types or enumeration types are supported in this context.</p>
  </blockquote>
  
  <p>Interface for type being queried:</p>
  
  <pre><code>interface IEntity&lt;TId&gt;
  {
      TId id { get; set; }
  }
  </code></pre>
  
  <p>The exception is thrown here:</p>
  
  <pre><code>public virtual TEntity Get&lt;TEntity, TId&gt;(TId id) where TEntity : class, IEntity&lt;TId&gt;
  {
      return dbContext.Set&lt;TEntity&gt;().FirstOrDefault(e =&gt; e.Id.Equals(id));
  }
  </code></pre>
  
  <p>The function will only work if <code>TId</code> is constrained as <code>struct</code>. How can I include <code>string</code> as supported type? If it's not possible, is it possible to accomplish the task a different way?</p>