我有一个用实体框架映射的数据库,
我需要实现一个通用方法来根据我传递的参数获取项目列表:
getGenericList("product"); // returns the list of products
getGenericList("customer"); // returns the list of customers
Run Code Online (Sandbox Code Playgroud)
我需要动态获取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>();
}
Run Code Online (Sandbox Code Playgroud)
我dbSets首先由 EF 代码自动生成:
public DbSet<Product> Products { get; set; }
public DbSet<Custommer> Custommers { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的getDBSet(entityType)方法是在上下文中实现的,如下所示:
public DbSet<T> getDBSet<T>(string entityName) where …Run Code Online (Sandbox Code Playgroud) linq-to-entities dynamic-data auto-generate entity-framework-5