I'm using EF 4 and I want to create generic method, which will allow me to load data from tables with only two columns - which I am sure exists in every table and entity. My problem is that I don't know how to get list of entities only knowing type of entity.
Dictionary<long, string> GetList<T>() where T : System.Data.Objects.DataClasses.EntityObject
{
Dictionary<long, string> list = new Dictionary<long, string>();
//var entityList = this.context.GetEntitiesByType(T);
//
foreach (var entity in entityList)
{
list.Add(typeof(T).GetProperty("Id").GetValue(entity, null),
typeof(T).GetProperty("Name").GetValue(entity, null))
}
return list;
}
Is it possible to obtain this list ? As a workaround I could get table name by using method
dbContext.GetTableName<T>();
and try to execute sql query, but it seems like bad idea. Any thoughts ?
var entityList = this.context.Set<T>()