Reputation: 21764
I'm trying to write some extensions on top of EF and I need to be able to inspect a code first ObjectContext and retrieve the entity types in it. I feel like this should be available somewhere in the metadata workspace, but I'm not sure where to look. I'm using EF 5.
Note that we don't use any kind of code generation to create our contexts, nor do we put type-specific DbSet accessors on the DbContext base class. Thus, I can't simply reflect over the DbContext/ObjectContext to look that such properties.
Upvotes: 6
Views: 1598
Reputation: 31610
I think this should work:
var objectItemCollection =
(ObjectItemCollection )((IObjectContextAdapter)ctx)
.ObjectContext.MetadataWorkspace.GetItemCollection(DataSpace.OSpace);
foreach(var entityType in objectItemCollection.GetItems<EntityType>())
{
Console.WriteLine(objectItemCollection.GetClrType(entityType).FullName);
}
Upvotes: 7