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




