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

  • SEARCH
  • Home
  • 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 7681379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:19:41+00:00 2026-05-31T18:19:41+00:00

Is it possible in Entity Framework to make a custom converter method for converting

  • 0

Is it possible in Entity Framework to make a custom converter method for converting an integer into an entity through an explicit conversion?

I did some research on this, and I don’t know where to start.

Here’s an example of what I’m trying to do.

int activeTeacherId = 38;
Teacher activeTeacher = (Teacher)activeTeacherId;

Edit 1 After some quick research, I figured out that I probably need to do something with the EntityObject if I need everything to be truly generic and flexible. However, I’m not sure how.

Edit 2 From my own experience, I managed to create the following code. However, for obvious reasons, I can’t get “this” inside a static context.

If I could just somehow get the type of the object that it’s being converted into (since it’s not always being converted into an EntityObject, but sometimes a Person or a Teacher), then it would theoretically work.

public class EntityObject : System.Data.Objects.DataClasses.EntityObject
{
    public static explicit operator EntityObject(int id)
    {
        var container = ModelContainer.Instance;

        var thisType = this.GetType(); //this can't be done from a static context, so how do we retrieve the type that we are converting into?
        var containerType = typeof (ModelContainer);

        dynamic setProperty = typeof (ModelContainer).GetProperty(thisType.Name + "Set");

        ObjectSet<dynamic> set = setProperty.GetValue(container);

        return set.FirstOrDefault(o => o.Id == id);
    }
}
  • 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-05-31T18:19:42+00:00Added an answer on May 31, 2026 at 6:19 pm

    I finally did it! Based on Peter’s suggestions (see his comment on my question), I have created an extension method on “int” which allows me to perform the actions I am looking for.

    I’m quite proud of this algorithm since I figured out most of it myself. It involves some serious reflection stuff, dynamic creation of lambda expressions, and the evaluation of them. But it works!

    Any suggestions to make it shorter or better would be appreciated.

    public static class EntityObjectExtensions
    {
    
        private static int Id;
    
        public static T ToEntity<T>(this int id) where T : class
        {
    
            lock(typeof(ModelContainer))
            {
    
                Id = id;
    
                var container = ModelContainer.Instance;
    
                var thisType = typeof (T);
                while (thisType.BaseType != typeof (EntityObject))
                {
                    thisType = thisType.BaseType;
                }
    
                var setProperty = typeof (ModelContainer).GetProperty(thisType.Name + "Set");
    
                dynamic set = setProperty.GetValue(container);
    
                var firstOrDefaultMethod =
                    typeof (Enumerable).GetMethods().FirstOrDefault(m => m.Name == "FirstOrDefault" && m.GetParameters().Count() == 2);
                var firstOrDefaultGenericMethod = firstOrDefaultMethod.MakeGenericMethod(thisType);
    
                var lambda = typeof (Func<,>);
                var genericLambda = lambda.MakeGenericType(thisType, typeof (bool));
    
                var lambdaParameter = (Func<T, bool>) Delegate.CreateDelegate(genericLambda, typeof(EntityObjectExtensions).GetMethod("Compare", BindingFlags.Static | BindingFlags.NonPublic));
    
                dynamic item = firstOrDefaultGenericMethod.Invoke(null, new object[] {set, lambdaParameter});
    
                return (T) item;
    
            }
    
        }
    
        private static bool Compare(dynamic item)
        {
    
            lock (typeof(ModelContainer))
            {
    
                return item.Id == Id;
    
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to make some work in parallel with entity framework for following
Is it possible to use Microsoft Entity Framework with Oracle database?
Is it possible to refresh only one entity in entity framework designer? Every time
I'm trying to use Entity Framework and it put it's connection string into app.config.
Is it possible for Entity Framework 4.0 to have an association/navigation property based off
I'm currently looking into possible ways we can refactor our codebase, to make it
Possible Duplicate: Entity Framework Code First: decimal precision I'm using Linq-to-Entities with the EntityFramework
Possible Duplicate: Conditional Linq Queries Using Entity Framework 4.0 I have a search condition
Possible Duplicate: What is the best way to compare two entity framework entities? I
Possible Duplicate: Entity Framework Include() strongly typed Ok, I've got an entity framework query

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.