Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
Ok here is the deal, I have a Generic Base Entity as shown below:
public abstract class Entity<Tkey> : IEntity<Tkey>
where Tkey:IComparable
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public abstract Tkey Id { get; set;}
}
I prefer this, because you know, some table's Identity column type is Guid, some int and some long.
So, in my Generic repository I have some method like this one:
public class FRepository<TEntity,Tkey>:IDisposable where TEntity : Entity<Tkey>, new()
where Tkey:IComparable
{
public TEntity Find(Tkey id)
{
return this.GetAll().Where(e=> e.Id.CompareTo(id)==0).FirstOrDefault();
}
}
GetAll() method returns IQueryable<TEntity>.
But when I use Linq to find specific entity, it throws exception as I write begining of this post.
Note: The entity which I am trying to find has Id with int type, so it is a primitive type...
Note-1: I can't able to use '==' operator with generic type TKey. I get compilation error.
Note-2: I also tried this Where(e=>e.Id.GetHashCode()==id.GetHashCode())
. This throws another error(run-time).
LINQ to Entities does not recognize the method 'Int32 GetHashCode()' method, and this method cannot be translated into a store expression.