1
Vote

Bug in GenericController.GetEntity(object id) when using a Guid

description

First of all, great work with this library! I'm building a website using Linq To Sql and this library has been a lifesaver when dealing with the DataContext issues.
 
The GetEntity method will throw a SqlException because the Guid value is not quoted in the select query. I found a solution around the error, but i'm not 100% sure if this will work in all situations (Currently it's only tested having a Guid as primary key).
 
I replaced the body of the GetEntity method with:
 
ParameterExpression paramExpr = Expression.Parameter(typeof(TEntity), "e");
Expression<Func<TEntity, bool>> predicate = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(Expression.Property(paramExpr, PrimaryKeyDBColumnName), Expression.Constant(id)), paramExpr);
return EntityTable.FirstOrDefault(predicate);
 
Any feedback on this solution is greatly appreciated

comments

taliesins wrote Sep 30, 2009 at 10:22 AM

I suspect that the problem lies in the following line.

Change:
ConstantExpression value = Expression.Constant(Convert.ChangeType(id, primaryKey.Type));

To:
ConstantExpression value = Expression.Constant(Convert.ChangeType(id, primaryKey.Type), primaryKey.Type);

wrote Feb 14, 2013 at 1:42 AM