Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the corresponding POCO object Type from an Entity Framework Metadata EntityType object?

I have annotated my Entity Framework Code First objects with some extra metadata, such as the DataTypeAttribute or possibly new, custom attributes. A version of this code (from http://www.minddriven.de/index.php/technology/dot-net/web/asp-net-mvc/check-data-annotations-from-code) works well to read the attributes once I have the EF Code First POCO object's Type object.

However, I cannot figure out how to go from the MetadataWorkspace, where I find all the entities:

ObjectContext objContext = ((IObjectContextAdapter)this).ObjectContext;
MetadataWorkspace mw = objContext.MetadataWorkspace;
var entities = mw.GetItems<EntityType>(DataSpace.OSpace);

to the POCO class Types I need to reflect on the Attributes.

How do I get from an EntityType to the POCO object or its proxy? Or alternatively, how can I find all the POCO objects in the context without GetItems()?

Relevant Links:

  • ASP.NET MVC Quick Tip: Check Data Annotations from code
  • How to read Custom Attributes using reflection set by Fluent API in EF 4.1
  • MSDN link about getting entities from proxies
like image 527
Scott Stafford Avatar asked Mar 20 '26 03:03

Scott Stafford


1 Answers

There might be a direct way to do this but you can get the type from the full name

var types = from entity in entities
            select Type.GetType(entity.FullName);
like image 88
Alaa Masoud Avatar answered Mar 21 '26 17:03

Alaa Masoud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!