I have a database mapped with Entity Framework,
I need to implement a generic method for getting a a list of items based on a parameter that I pass:
getGenericList("product"); // returns the list of products
getGenericList("customer"); // returns the list of customers
I need to dynamically get the dbSet
. My method is implemented like this:
public static List<object> getGenericList(string entityType)
{
List<object> myDynamicList = new List<object>();
using (cduContext db = new cduContext())
{
DbSet dbSet = db.getDBSet(entityType);
var myDynamicList = dbSet.Select(p => p).ToList();
}
return new List<object>();
}
my dbSets
are auto-generated by EF code first :
public DbSet<Product> Products { get; set; }
public DbSet<Custommer> Custommers { get; set; }
my getDBSet(entityType)
method is implemented in the context, like this:
public DbSet<T> getDBSet<T>(string entityName) where T : class
{
switch (entityName)
{
case "product":
return Products;
case "custommer":
return Custommers;
I then got this error:
Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Data.Entity.DbSet'
Any Idea please !?
N.B. , the method Set()
of the dbContext
is not OK; the type should be explicitly given ...