The drawback of using ORM layers is that often the error messages are very cryptic, and it takes some getting used to in order to find the (often deceptively) simple solution.
This case was an Entity Framework wrapper around a SQL Server database where the primary and foreign keys were all GUIDs, and some of the foreign keys were optional.
So the generated model has a mixed use of Guid? and Guid data types.
Below is the full stack trace, but here is the exception class and message:
System.NotSupportedException: Unable to create a constant value of type ‘System.Object’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.
The exception is caused by a piece of code like this:
public static long CountChildren(ParentEntity parentEntity)
{
using (EntitiesObjectContext objectContext = new EntitiesObjectContext())
{
Guid? parentId = parentEntity.ID;
if (null == parentId)
throw new ArgumentNullException("parentEntity.Id");
IQueryable<ChildEntity> ChildEntitys =
from content in objectContext.ChildEntity
where content.ParentID.Equals(parentId)
select content;
long result = ChildEntitys.Count(); // BOOM!
return result;
}
}
The stack trace at the end of this post contains a truckload of ExpressionConverter lines. Since the LINQ expression contained only one WHERE clause, the mentioning of the list of primitive types in the message (Int32, String, and Guid) made me change the code into the one below.
Read the rest of this entry »