Is there an Entity Framework equivalent of Linq2Sql's DataContext.GetTableTEntity> (ObjectContext.CreateQueryT>)?

.net c# entity-framework

Question

I'm seeking for a replacement for theDataContext.GetTable<TEntity> Entity Framework, I've located theObjectContext.CreateQuery<T> technique, however it differs fromDataContext.GetTable<TEntity> because it requires a querystring to function.

Is it possible to get an IQueryable object for a table using the entity type without providing a querystring?

*EDIT: Added code snippet*
This is a sample of a linq2sql-compatible Repository class that I developed. Unable to utilizeObjectContext.[TableName] because it would cease to be general.

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..)*
I'm use the following:

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

As long as the class name and table name are the same, this works. As soon as I start utilizing distinct objects for the same table, this will become an issue for me.

I hope I was clear, and I appreciate your patience.
Marco:)

1
8
7/28/2009 8:31:01 AM

Accepted Answer

In reality, the EF designer employsCreateQuery using static references using hard-coded strings. You'll find something similar if you go further into the designer file:

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;

Since you may utilize the same entity type for many entity sets, there isn't a perfect answer in a technical sense. However, you might attempt it the old-fashioned way:

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);
}
6
8/18/2011 8:36:20 PM

Popular Answer

public IQueryable GetTable<T>(T entity) where T : class
{
    return context.CreateObjectSet<T>();
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow