Quick Trick for forcing MetadataWorkspace ItemCollections to load

This may only mean something to about 10 people on the planet, but I thought I'd share it anyway.

If you are using Entity Framework's MetadataWorkspace to dig around in the model's structure, the CSpace (schema from the conceptual layer aka CSDL) is readily available however, the other collections, SSpace (ssdl) and CSSpace( msl) are not loaded until they are needed. Well, trying to read them with the MetadataWorkspace doesn't count as being needed. They are needed during query compilation, then again at object materialization.

Unfortunately, there's not method to just say "hey, get me the MSL schema, please", though there are some factory methods deep down in the API that are a PIA to use.

SO instead, what I've been using is just a quick method that creates an ObjectQuery and then forces query compilation by requesting ToTraceString. Then throw away the query. No need to execute it.

I created this method in a Partial Class for a oarticular EntityContainer class.

Private Sub forceDataSpacestoLoad()
Dim tracestring=  _
 CreateQuery(Of Contact)("AWEntities.Contacts").ToTraceString
 'Note that the ESQL is a shortcut
End Sub

Now when I need to use the mapping or store collections, if the one I need is not loaded, I can force it.

If Not mdw.TryGetItemCollection _
 (DataSpace.CSSpace, mappingCollection)Then
forceDataSpacestoLoad()
End If

Once loaded, ItemCollections stay in the application cache.

You could create something more generic and make it an extension method of ObjectContext. HEre, I just grab a random entity to build a query from and cast the whole thing back to an EntityObject.

 If Not randomentityType Is Nothing Then
   Dim esetName = mdw.GetEntitySetFullName(randomentityType)
   Dim tracestring = CreateQuery(Of EntityObject)(esetName).ToTraceString
 Else
   Throw New InvalidOperationException("Could not force metadata to load")
 End If

Now you'll probably want to know where GetEntitySetFullName comes from. It's one of the many extension methods I've created while writing my book.

Here ya go!

<Extension()> _Public Function GetEntitySetFullName _
  (ByVal mdw As MetadataWorkspace, ByVal entityName As String) As String
  Dim entContainer = mdw.GetItems(Of EntityContainer)(DataSpace.CSpace).First
  Dim entsets = From eset In entContainer.BaseEntitySets _
                Where eset.ElementType.Name = entityNameDim container
  Name = entsets.First.EntityContainer.Name
  Return containerName & "." & entsets.First.Name
End Function

#1 Ido Flatow on 8.20.2008 at 3:20 PM

Two weeks ago I saw this post and after starting to read it came to realize I wan't one of the 10 people who need it.Today, I've worked on dynamically altering the sql query of the DefiningQuery element, and after playing with the MetadataWorkspace and the SSpace to find a way to download the SSDL I stumbled upon the post again.It seems that there are now 11 people on the planet it means something to.Thanks for the tip, I'll use it tommorow.

#2 Cankut Eskin on 6.27.2009 at 5:43 PM

Hi i'm #12, thanks :)

#3 Borislav T on 8.23.2009 at 6:00 PM

Thanks for the info Julie!

I was really wondering how to get a hold of this info.

Also, here's another post that is somewhat related to getting this sort of information:

stackoverflow.com/.../how-to-get-stor

#4 El Guapo on 8.28.2009 at 8:11 PM

I am person # 13 who needed this. THANK YOU

Leave a Comment