Schema Crawling Question
Hey there team,
If I wanted to get the names of fields that comprise a key, what's the preferred way of doing that? :)
I've been crawling around in the MetadataWorkspace, and boy there's a lot there. Might be beneficial to have some sort of post that outlines the big concepts with this namespace...
Thanks!
Mike
Alright... through some serious spelunking, I've managed to fetch the TypeMetadata that's registered with the Product class. However, when I poll its .Members collection and do a search for "IsPartOfKey", it returns false.
I'm doing this on a Product class that comes with the NorthwindLib in the CTP. Is this functionality not implemented? A quick look at the constructor for the ClrProperty class shows that the "isPartOfKey" parameter is always false. I guess this is a "fix in future version?" :)
You're almost there, there is only one more step: the TypeMetadata for an entity type is actually a subtype called EntityType. So you can cast your TypeMetadata to EntityType. Once you have your EntityType, you can use the KeyMembers member to find out all the keys for a given entity.
Pablo Castro
ADO.NET Technical Lead
Microsoft Corporation
Awesome! Very cool. Thank you for your time and help... much appreciated. :)
It looks like that the Type that GetType sends back is not an EntityType. It's a ClrNonScalarType. Here is my code. Can you see what I'm doing wrong?
SchemaKey
key = ObjectContextHelper.CreateSchemaKeyFromClrAssembly(typeof (T).Assembly, MetadataWorkspace.OSpace );metadata = ObjectContext.MetadataWorkspace.GetType(
new NamedTypeKey( typeof (T).FullName, key ) )
as EntityType;
Where T is the type of my Entity (in this case, I'm testing on Product from NorthwindLib).
And CreateSchemaKeyFromClrAssembly is a straight rip from reflected code from the assembly (since it's internal).
Thanks!
Mike
You need to look for the type in "CSpace", not in "OSpace" (of course, those terms are not supposed to surface in the API :), so you may need to tweak the function a bit in order to find the entity type.
Pablo Castro
ADO.NET Technical Lead
Microsoft Corporation
Hmmm... I'm trying the following, but it's returning a null value:
SchemaKey key = new SchemaKey( typeof(T).Assembly.GetName().Name, MetadataWorkspace.OSpace, new SchemaVersion( 0, 0, 0, 0 ), "null" ); metadata = ObjectContext.MetadataWorkspace.GetType(
new NamedTypeKey( typeof (T).FullName, key ) );
What is the correct way of accessing the EntityType?
Thanks again for your help!