Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:15:05+00:00 2026-06-03T05:15:05+00:00

I have the following problem: I have a generic EF repository using DbContext designed

  • 0

I have the following problem: I have a generic EF repository using DbContext designed for Int or Guid entity keys, so i have a base entity class:

public class EntityBase<TKey> where TKey : struct, IComparable
{
    public virtual TKey Id { get; set; }
}
  • TKey will be provided as Int or Guid in derived classes.

When i run the code

public virtual void LoadEntity()
{
    TEntity entity = Repository.Get<TEntity, TKey>(e => object.Equals(e.Id, EntityId));
}

or

public virtual void LoadEntity()
{
    TEntity entity = Repository.Get<TEntity, TKey>(e => e.Id.CompareTo(EntityId) == 0);
}

where Entity is of type TKey and is set in derived classes as int, for example, I get the following error:

Unable to cast the type ‘System.Int32’ to type ‘System.Object’. LINQ to Entities only supports casting Entity Data Model primitive types.

Repository.Get just pass predicate parameter as a filter for a Where call for DbSet repository;

I understand the error – EF tries to translate to SQL statement and does not know how to treat the object comparison. But I don’t know how to rewrite base class and/or LoadEntity() function to allow EF to operate with primitive types.
Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:15:07+00:00Added an answer on June 3, 2026 at 5:15 am

    I think there is an easy way around it but it is a hack. Let me stress it again – it really is a hack. You can try this:

    Repository.Get<TEntity, TKey>(e => (object)e.Id == (object)EntityId);
    

    The code above in general should not work. In the CLR world the values would be boxed and will be compared by references. Even if the boxed values were the same the references would be different and therfore the result will be false. However EF queries are not executed by the CLR but translated to SQL. As a result the query will be translated to something like: WHERE Id = {EntityId} which is what you need. Again, using this requires understanding how and why this stuff works and is probably a bit risky. However since there is a hack there should be a cleaner solution. In fact the clean (and not easy solution here) is to build the above expression manually. Here is an example (Sorry I am not using exactly your entities):

        private static TEntity GetEntity<TEntity, TKey>(Expression<Func<TEntity, TKey>> property, TKey keyValue)
            where TKey : struct
            where TEntity : BaseEntity<TKey>
        {
            using (var ctx = new Context2())
            {
                var query = Filter(ctx.Set<TEntity>(), property, keyValue);
                return query.First();
            }
        }
    
    
        private static IQueryable<TEntity> Filter<TEntity, TProperty>(IQueryable<TEntity> dbSet,
                                                                      Expression<Func<TEntity, TProperty>> property,
                                                                      TProperty value)
            where TProperty : struct
        {
    
            var memberExpression = property.Body as MemberExpression;
            if (memberExpression == null || !(memberExpression.Member is PropertyInfo))
            {
                throw new ArgumentException("Property expected", "property");
            }
    
            Expression left = property.Body;
            Expression right = Expression.Constant(value, typeof (TProperty));
    
            Expression searchExpression = Expression.Equal(left, right);
            var lambda = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(left, right),
                                                                new ParameterExpression[] {property.Parameters.Single()});
    
            return dbSet.Where(lambda);
        }
    

    Note that in the Filter method I build a filter expression that I can compose on. In this example the effective query looks something like this DbSet().Where(e => e.Id == idValue).First() (looks similar to the hack above) but you can use other linq operators on top of this query (including invoking Filter method on the result of the Filter method to filter by multiple criteria)

    I defined the entities and the context as follows:

    public class BaseEntity<TKey> where TKey : struct
    {
        public TKey Id { get; set; }
    }
    
    public class EntityWithIntKey : BaseEntity<int>
    {
        public string Name { get; set; }
    }
    
    public class EntityWithGuidKey : BaseEntity<Guid>
    {
        public string Name { get; set; }
    }
    
    public class Context2 : DbContext
    {
        public DbSet<EntityWithIntKey> EntitiesWithIntKey { get; set; }
    
        public DbSet<EntityWithGuidKey> EntitiesWithGuidKey { get; set; }
    }
    

    You invoke the GetEntity method like this: var e2 = GetEntity(e => e.Id, guidKey);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in Java: I have a base class and a
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have the following problem when using java generic. Basically I need to do
I have the following generic classes: class Base<T> where T : ... { ...
Here is my problem in C# : I have the following classes: public class
i have written a generic repository for my base windows which have a problem
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
If i have the following entity: public class PocoWithDates { public string PocoName {
I have the following problem using subversion: I'm currently working on the trunk of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.