我正在使用 Entity Framework 6.1、DbContext、数据库优先。 我知道我可以通过以下名称获取 DBset:context.Myobj1、context.Myobj2.....
但是有没有可能做一个 for...each 循环并在 DbContext 上一个接一个地获取所有 DbSet(of Tentity)?
最佳答案
您需要发现实体的类型。这取决于您的应用程序。您可以使用反射来发现这些类型。或者您可以对实体的类型进行硬编码。
在此之后你可以遍历集合:
var types = new [] { typeof(User), typeof(Role) }; // just example
foreach(var type in types)
{
foreach(var entity in context.DbSet(type))
{ ... }
}
关于entity-framework - 循环遍历 DBContext 上的所有 DBSet(属于 TEntity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30881896/