1

Can i load entity data by name as:

using (var db = new DatabaseEntities())
            {
                db["NameOfEntity"].ToList();
            }

Thanks

Hoang Tung
  • 23
  • 1
  • 6

1 Answers1

0

You can get them by Type

public class DatabaseEntities : DbContext
{

    public object GetList(string entityName)
    {
        return GetList(Type.GetType(entityName));
    }

    private List<TEntity> GetList<TEntity>(TEntity type) where TEntity : class
    {
        return Set<TEntity>().ToList();
    }
}
Kahbazi
  • 14,331
  • 3
  • 45
  • 76
  • Well, if i use say "Employee" for type it'll get type object for Employee and use this type object as generic parameter. Then it'll execute line `Set().ToList();` and it'll obviously throw – Fabjan Aug 02 '16 at 08:56
  • Thank @Arvin, it's worked – Hoang Tung Aug 02 '16 at 09:20