我有一个数据库映射实体框架的所有实体,实体框架动态DbSet为
我需要实现获得基于一个参数,我通过项目的AA名单的一般方法:
getGenericList("product"); // returns the list of products
getGenericList("customer"); // returns the list of customers
我需要动态获取dbSet
。我的方法是实施这样的:
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>();
}
我dbSets
通过EF代码首先是自动生成的:
public DbSet<Product> Products { get; set; }
public DbSet<Custommer> Custommers { get; set; }
我getDBSet(entityType)
方法的背景下实现的,就像这样:
public DbSet<T> getDBSet<T>(string entityName) where T : class
{
switch (entityName)
{
case "product":
return Products;
case "custommer":
return Custommers;
然后我得到这个错误:
Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Data.Entity.DbSet'
任何想法请!?
N.B. ,dbContext
的方法Set()
不正确;该类型应该明确给出...
您可能想要在实际代码中更正“custommer” - >“customer”的拼写。 – MEMark