Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Entity Framework have an equivalent of DataContext.GetTable<TEntity> from Linq2Sql (ObjectContext.CreateQuery<T>?)

I'm looking for an equivalent of the DataContext.GetTable<TEntity> in Entity Framework. I've found the ObjectContext.CreateQuery<T> method but it is different from DataContext.GetTable<TEntity> since it needs a querystring to work.

Is there a way to get an IQueryable object for a table using the entity type without specifying the querystring?

*EDIT: Added code snippet*
This is a snippet of a Repository class I've implemented that works with linq2sql. I can't use ObjectContext.[TableName] because it wouldn't be generic anymore.

public class BaseRepository<TClass> : IDisposable
        where TClass : class
    {
        protected BaseRepository(DataContext database)
        {
            _database = database;
        }
        ...

        public IQueryable<TClass> GetAllEntities()
        {
            IQueryable<TClass> entities = _database.GetTable<TClass>();
            return entities;
        }

        public IQueryable<TClass> GetEntities(Expression<Func<TClass, bool>> condition)
        {  
            IQueryable<TClass> table = _database.GetTable<TClass>();
            return table.Where(condition);    
        }

*EDIT: Added my solution (so far..)*
This is what I'm using:

public IQueryable<TClass> GetEntities(Expression<Func<TClass, bool>> condition)
{  
    IQueryable<TClass> table = _database.CreateQuery<TClass>(typeof(TClass).Name);
    return table.Where(condition);    
}

This works as long as the class name is the same of the table name. This will became a problem for me when I'll start using different objects for the same table.

I hope I've been clear, thanks in advance,
Marco :)

like image 423
marcob Avatar asked Jul 27 '09 19:07

marcob


2 Answers

Actually, the EF designer itself uses CreateQuery with hard-coded strings for the static references. If you dig into the designer file you'll see something like this:

public global::System.Data.Objects.ObjectQuery<Customers> Customers
{
    get
    {
        if ((this._Customers == null))
        {
            this._Customers = base.CreateQuery<Customers>("[Customers]");
        }
        return this._Customers;
    }
}

private global::System.Data.Objects.ObjectQuery<Customers> _Customers;

Technically there's no perfect solution because you can use the same entity type for different entity sets. But you can give it the old college try:

public IQueryable<TEntity> GetEntities<TEntity>()
{
    Type t = typeof(TEntity);
    var edmAttr = (EdmEntityTypeAttribute)Attribute.GetCustomAttribute(t,
        typeof(EdmEntityTypeAttribute), false);
    if (edmAttr == null)  // Fall back to the naive way
    {
        return context.CreateQuery<TEntity>(t.Name);
    }
    var ec = context.MetadataWorkspace.GetEntityContainer(
        context.DefaultContainerName, DataSpace.CSpace);
    var entityType = context.MetadataWorkspace.GetType(edmAttr.Name,
        edmAttr.NamespaceName, DataSpace.CSpace);
    var es = ec.BaseEntitySets.First(es => es.ElementType == entityType);
    return context.CreateQuery<TEntity>(es.Name);
}
like image 169
Aaronaught Avatar answered Nov 11 '22 20:11

Aaronaught


public IQueryable GetTable<T>(T entity) where T : class
{
    return context.CreateObjectSet<T>();
}
like image 32
MarkWalls Avatar answered Nov 11 '22 19:11

MarkWalls