Enumerating column information from an Entity Framework class

Alw1n
Alw1n used Ask the Experts™
on
Hi,
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?
Comment
Watch Question

Do more with

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

Author

Commented:
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
                Next

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

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

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


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

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

        Next

Commented:
hi,
I am new to LINQ and just tried googlying and found the following discussion
http://social.msdn.microsoft.com/Forums/en-SG/adodotnetentityframework/thread/cbaaa2e2-5f36-4b18-837c-e1f93aadb571

see if that helps you.

Thanks and regards,
Venki
Commented:
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

        'sModel:
        'ConceptualModels
        'StorageModels
        '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

Commented:
OOPs my mistake I am sorry for that.

Author

Commented:
:-) 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