Is it possible with Generics to get an object from my EntityFramework without knowing the type?
I\'m thinking of something along the lines of:
public
Here's the method I use to get an Entity object by ID.
public TEntity GetByKey(object keyValue) where TEntity : class
{
EntityKey key = GetEntityKey(keyValue);
object originalItem;
if (ObjectContext.TryGetObjectByKey(key, out originalItem))
{
return (TEntity)originalItem;
}
return default(TEntity);
}
You may find this a more robust solution than some of those provided above. I've used it as part of a generic repository in a lot of projects. Hope it helps you out.