How can I get a list of entity types from an ObjectContext at runtime using EF code?

I'm trying to write some extensions on top of EF, and I need to be able to check the ObjectContext code first and extract entity types in it. I feel it should be available somewhere in the metadata workspace, but I'm not sure where to look. I am using EF 5.

Please note that we do not use any code generation to create our contexts, and we do not add access types to the DbSet type in the base DbContext class. So, I can’t just think about DbContext / ObjectContext to see what such properties are.

+6
source share
1 answer

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); } 
+6
source

Source: https://habr.com/ru/post/954373/


All Articles