Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe the problem is that Linq/NHibernate is trying to map IEntity.Id to a table column instead of TEntity.Id. I had this problem with a LinqToSql repository implementation. The way around it was to use an expression like this:</p> <pre><code>private static Expression&lt;Func&lt;TEntity, bool&gt;&gt; GetFindExpression(string propertyName, object value) { ParameterExpression parameterExpression = Expression.Parameter(typeof (TEntity), "id"); MemberExpression propertyExpression = Expression.Property(parameterExpression, propertyName); Expression bodyExpression = Expression.Equal(propertyExpression, Expression.Constant(value)); return Expression.Lambda&lt;Func&lt;TEntity, bool&gt;&gt;(bodyExpression, parameterExpression); } </code></pre> <p>So this would change Get(id) to:</p> <pre><code>public TEntity Get(int id) { var entities = Query.Where(GetFindExpression("Id", id)); return entities.FirstOrDefault(); } </code></pre> <p><strong>Update:</strong></p> <p>If you don't want to deal with expressions (they can be tricky!), you could use the Dynamic LINQ library as described by Scott Guthrie here: <a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx</a></p> <p>That would change Get(id) to:</p> <pre><code>public TEntity Get(int id) { var entities = Query.Where("Id = @0", id); return entities.FirstOrDefault(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload