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

[346 byte] By [Mike-EEE] at [2008-2-16]
# 1

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?" :)

Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 2

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

pablo-ms at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 3

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

Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 4

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

pablo-ms at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 5

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!

Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 6
Sigh... I obviously cannot read. Let me try CSpace. :) (I tried deleting that post but it wanted to delete the whole thread? weird).
Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...
# 7

SchemaKey key = new SchemaKey( string.Concat( typeof(T).Assembly.GetName().Name, ".Model" ), MetadataWorkspace.CSpace, new SchemaVersion( 0, 0, 0, 0 ), "null" );

metadata = ObjectContext.MetadataWorkspace.GetType( new NamedTypeKey( typeof (T).FullName, key ) );

Is the answer!

Mike-EEE at 2007-8-31 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...

Visual Studio Orcas

Site Classified