如何查询实现接口的所有表

我为我的一些实体类实现了一个接口:

public partial class Order : IReportable { public string TableName { get { return "Order"; } } } public partial class Client: IReportable { public string TableName { get { return "Client"; } } } public interface IReportable { string TableName { get; } } 

然后我把它添加到DbContext:

 public virtual DbSet IReportable { get; set; } 

当我尝试查询实现此接口的所有表时(如此处所示):

 var result = from reportabletable in db.IReportable where reportabletable.TableName == table_name select reportabletable 

我得到以下exception:

未映射“Report.DataAccess.IReportable”类型。 使用Ignore方法或NotMappedAttribute数据批注检查未明确排除类型。 validation类型是否已定义为类,不是原始类型还是通用类型,并且不从EntityObjectinheritance。

我会选择这样的东西:

创建此扩展方法

 public static class DbContextExtensions { public static IEnumerable SetOf(this DbContext dbContext) where T : class { return dbContext.GetType().Assembly.GetTypes() .Where(type => typeof(T).IsAssignableFrom(type) && !type.IsInterface) .SelectMany(t => Enumerable.Cast(dbContext.Set(t))); } } 

并像这样使用它:

 using (var db = new dbEntities()) { var result = from reportabletable in db.SetOf() where reportabletable.TableName == table_name select reportabletable } 

EF不喜欢将接口直接映射到表。 您可以通过使用通用存储库来解决这个问题,如此处所述!

然后使用存储库方法并提供要查询的表的类型。 类似于: myRepo.GetAll();

获取inheritance该接口的类并为所有接口运行查询:

 var types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(mytype => mytype .GetInterfaces().Contains(typeof(myInterface))); foreach (var mytype in types) { // aggregate query results } 

希望这可以帮助! 可能有更优雅的解决方案

首先,MarcGravell评论的是钱。 由您决定要查询哪个表。 就个人而言,我浏览了实现接口或具有自定义属性的poco类型列表。 但是,如果您只想通过DBContext,那么这里有一些扩展可以让您访问“名称”。 您仍然需要一次一个地访问该部分上下文。

你可以再次通过generics来做到这一点,但你可以直接按照你的建议去做。
您需要迭代一个类型列表。 例如:

ReportRespository:BaseRespository,其中t:IReport

检查组件的某些类型和属性,例如

  ///  /// POCOs that have XYZ Attribute of Type and NOT abstract and not complex ///  ///  public static List GetBosDirDBPocoList() { var result = new List(); // so get all the Class from teh assembly that public non abstract and not complex foreach (var t in Assembly.GetExecutingAssembly().GetTypes() .Where(t => t.BaseType != null && t.IsClass && t.IsPublic && !t.IsAbstract && !t.IsComplexType() && t.GetMyAttribute() != null)) { result.Add(t); } } return result; } public static GetMyAttribute(this Type T) { var myAttr= T.GetCustomAttributes(true) .Where(attribute => attribute.GetType() .Name == "XYZAttr").Cast().FirstOrDefault(); return myAttr; } 

扩展

  public static class DalExtensions { // DbSet Names is the plural property name in the context public static List GetModelNames(this DbContext context) { var propList = context.GetType().GetProperties(); return GetDbSetNames(propList); } // DbSet Names is the plural property name in the context public static List GetDbSetTypeNames() where T : DbContext { var propList = typeof (T).GetProperties(); return GetDbSetNames(propList); } // DBSet Types is the Generic Types POCO name used for a DBSet public static List GetModelTypes(this DbContext context) { var propList = context.GetType().GetProperties(); return GetDbSetTypes(propList); } // DBSet Types POCO types as IEnumerable List public static IEnumerable GetDbSetPropertyList() where T : DbContext { return typeof (T).GetProperties().Where(p => p.PropertyType.GetTypeInfo() .Name.StartsWith("DbSet")) .Select(propertyInfo => propertyInfo.PropertyType.GetGenericArguments()[0]).ToList(); } // DBSet Types is the Generic Types POCO name used for a DBSet public static List GetDbSetTypes() where T : DbContext { var propList = typeof (T).GetProperties(); return GetDbSetTypes(propList); } private static List GetDbSetTypes(IEnumerable propList) { var modelTypeNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet")) .Select(p => p.PropertyType.GenericTypeArguments[0].Name) .ToList(); return modelTypeNames; } private static List GetDbSetNames(IEnumerable propList) { var modelNames = propList.Where(p => p.PropertyType.GetTypeInfo().Name.StartsWith("DbSet")) .Select(p => p.Name) .ToList(); return modelNames; } } 

}