【发布时间】:2013-04-10 10:06:30
【问题描述】:
我有一个用实体框架映射的数据库,
我需要实现一个通用方法来根据我传递的参数获取项目列表:
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;
然后我得到了这个错误:
无法将类型“System.Data.Entity.DbSet”隐式转换为“System.Data.Entity.DbSet”
有什么想法吗!?
注意,dbContext的方法Set()不行;类型应该明确给出...
【问题讨论】:
-
您可能需要更正实际代码中“custommer”->“customer”的拼写。
标签: linq-to-entities entity-framework-5 dynamic-data auto-generate