Created
August 13, 2010 20:55
-
-
Save stevehorn/523534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Function GetById(ByVal id As Integer) As T Implements IDataLayer(Of T).GetById | |
Dim entityType As Type = GetType(T) | |
Dim entityTypeName As String = entityType.ToString | |
Dim keyProperty As String = entityTypeName.Substring(entityTypeName.LastIndexOf(".") + 1) + "Id" | |
Dim container = Context.MetadataWorkspace.GetEntityContainer(Context.DefaultContainerName, Metadata.Edm.DataSpace.CSpace) | |
Dim entitySetName As String = (From meta In container.BaseEntitySets _ | |
Where meta.ElementType.Name = entityType.Name _ | |
Select meta.Name).First() | |
Dim entitySetFullName As String = String.Format("{0}.{1}", container.Name, entitySetName) | |
Dim entityKeyValues As IEnumerable(Of KeyValuePair(Of String, Object)) = _ | |
New KeyValuePair(Of String, Object)() {New KeyValuePair(Of String, Object)(keyProperty, id)} | |
Dim key As New EntityKey(entitySetFullName, entityKeyValues) | |
Return DirectCast(Context.GetObjectByKey(key), T) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment