Enumerating column information from an Entity Framework class

Alw1n used Ask the Experts™
I have an entity framework model of an sql database and I would like to programmatically (vb.net) list all columns and the column spec for each tables - is this possible?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


I have done some digging around in the properties and came up with the (very rough) code below that get's quite a bit of info. What I can't find is:
- where the foreign key field info is stored  (can only find the foreign tbale name)
- where the column auto-number' info is stored

        Dim s As String
        Dim spk As String

        Dim pge As PangeaEntities = New PangeaEntities
        Dim mdw As Data.Metadata.Edm.MetadataWorkspace = pge.MetadataWorkspace

        For Each entities In mdw.GetItemCollection(Data.Metadata.Edm.DataSpace.CSpace)
            If entities.BuiltInTypeKind = Metadata.Edm.BuiltInTypeKind.EntityType Then

                Dim entity As System.Data.Metadata.Edm.EntityType = DirectCast(entities, System.Data.Metadata.Edm.EntityType)

                'Primary Keysmember
                spk = ""
                For Each pk In entity.KeyMembers
                    spk = spk & vbCrLf & "PK: " & pk.Name

                s = ""
                For Each member In entity.Members
                    If member.BuiltInTypeKind = Metadata.Edm.BuiltInTypeKind.EdmProperty Then
                        'DB Column
                        s &= vbCrLf & member.Name

                        s &= "|Datatype=" & member.TypeUsage.EdmType.Name

                        'Column settings
                        For Each facet In member.TypeUsage.Facets
                            s &= "|" & facet.Name & "=" & facet.Value

                    ElseIf member.BuiltInTypeKind = Metadata.Edm.BuiltInTypeKind.NavigationProperty Then
                        'Foreign key or other
                        'Not sure what else we can get
                    End If

                MsgBox(entity.Name & vbCrLf & spk & vbCrLf & s)
            End If


I am new to LINQ and just tried googlying and found the following discussion

see if that helps you.

Thanks and regards,
Hi tovvenki, that is actually my post :-).
For anyone interested, I managed to find the column & key info but unfortunately the index info does not appear to be stored in the entity model, which is a problem for me.
I tweaked some code i found on the net to read the different stores directly from the edmx file like this:

    Private Function ExtractEDMXContent(ByVal edmxFile As String, ByVal sModel As String) As XElement

        'Mappings (C-S Mappings)

        Dim csdlContent As XElement

        Dim edmxns As XNamespace = "http://schemas.microsoft.com/ado/2007/06/edmx"

        Dim edmns As XNamespace = "http://schemas.microsoft.com/ado/2006/04/edm"

        Dim ssdl As XNamespace = "http://schemas.microsoft.com/ado/2006/04/edm/ssdl"

        Dim cs As XNamespace = "urn:schemas-microsoft-com:windows:storage:mapping:CS"

        Dim edmxDoc As XDocument = XDocument.Load(edmxFile)

        If (Not edmxDoc Is Nothing) Then

            Dim edmxNode As XElement = edmxDoc.Element(edmxns + "Edmx")

            If (Not edmxNode Is Nothing) Then

                Dim runtimeNode As XElement = edmxNode.Element(edmxns + "Runtime")

                If (Not runtimeNode Is Nothing) Then

                    Dim ModelNode As XElement = runtimeNode.Element(edmxns + sModel)

                    If (Not ModelNode Is Nothing) Then

                        If sModel = "ConceptualModels" Then

                            csdlContent = ModelNode.Element(edmns + "Schema")

                        ElseIf sModel = "StorageModels" Then

                            csdlContent = ModelNode.Element(ssdl + "Schema")

                        ElseIf sModel = "Mappings" Then

                            Dim MappingsNode As XElement = ModelNode.Element(cs + "Mapping")
                            If (Not MappingsNode Is Nothing) Then
                                csdlContent = MappingsNode.Element(cs + "EntityContainerMapping")
                            End If                            

                        End If

                    End If

                End If

            End If

        End If

        Return csdlContent

    End Function

OOPs my mistake I am sorry for that.


:-) No Problem

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial