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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With