Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Get the primary key property name and the Foreign key property name of an arbitrary entity

Get the primary key property name and the Foreign key property name of an arbitrary entity

Scheduled Pinned Locked Moved Visual Basic
databasesysadminsql-serverhelptutorial
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dilkonika
    wrote on last edited by
    #1

    Hello ! I'm using entity Framework (Database First ) with a SQL Server Database. I have a situation where I have an object ( that is an entity object ) , but the type is known only on runtime ). I need to get the Primary key property name and the Foreign key property name ( for a specific relation) for example MyObj (ObId , name , val1)------- ( ObID is primary key ) MyChild (ChlID ,dt , MyObj_ID) --- ( CHLID is primary Key , Myobj_ID is foreign key) Dim obj1 ..... Get_PK(obj1) I want that the possible function Get_PK to return a string with the value : - "ObID" ( if the type of the object is MyOBJ) , or "ChlID" if the object is of the type MyChild. and the function Get_Parent(obj1,chl1) to return "MyOBj_ID" But of course it must be a general function that works with every class inside the entity. Actually I have 2 functions :

    Public Function Get_pk(ctx As MyEntities, entity As Object) As String
    Dim objectContext = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
    Dim t As Type = entity.GetType.BaseType
    Dim m As MethodInfo = objectContext.GetType().GetMethod("CreateObjectSet", New Type() {})
    Dim generic As MethodInfo = m.MakeGenericMethod(t)
    Dim st As Object = generic.Invoke(objectContext, Nothing)
    Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
    Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
    Dim keyNames As IEnumerable(Of String) = entitySet.ElementType.KeyMembers.Select(Function(k) k.Name)
    Return keyNames(0)
    End Function

    This is for Primary Key property , and is working , but is is slow ( I'm thinking if Entity framework has a built in method to get this directly ??? )

    Public Function Get_FK(ctx As MyEntities, entity As Object, parenttable As String) As String
    Dim fk = entity.MetadataWorkspace.GetItems(Of Metadata.Edm.AssociationType)(Metadata.Edm.DataSpace.CSpace).Where(Function(a) a.IsForeignKey)
    Dim fkname = fk.Where(Function(x) x.ReferentialConstraints(0).ToRole.Name = parenttable)
    Dim refcol = fkname.Select(Function(x) x.ReferentialConstraints(0).FromProperties(0).Name).First()
    return refcol
    End Function

    This is for the foreign key (related with a parenttable that is passed as parameter ). This is not working. I get an error in the first line because entity is a proxy object

    D 1 Reply Last reply
    0
    • D dilkonika

      Hello ! I'm using entity Framework (Database First ) with a SQL Server Database. I have a situation where I have an object ( that is an entity object ) , but the type is known only on runtime ). I need to get the Primary key property name and the Foreign key property name ( for a specific relation) for example MyObj (ObId , name , val1)------- ( ObID is primary key ) MyChild (ChlID ,dt , MyObj_ID) --- ( CHLID is primary Key , Myobj_ID is foreign key) Dim obj1 ..... Get_PK(obj1) I want that the possible function Get_PK to return a string with the value : - "ObID" ( if the type of the object is MyOBJ) , or "ChlID" if the object is of the type MyChild. and the function Get_Parent(obj1,chl1) to return "MyOBj_ID" But of course it must be a general function that works with every class inside the entity. Actually I have 2 functions :

      Public Function Get_pk(ctx As MyEntities, entity As Object) As String
      Dim objectContext = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
      Dim t As Type = entity.GetType.BaseType
      Dim m As MethodInfo = objectContext.GetType().GetMethod("CreateObjectSet", New Type() {})
      Dim generic As MethodInfo = m.MakeGenericMethod(t)
      Dim st As Object = generic.Invoke(objectContext, Nothing)
      Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
      Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
      Dim keyNames As IEnumerable(Of String) = entitySet.ElementType.KeyMembers.Select(Function(k) k.Name)
      Return keyNames(0)
      End Function

      This is for Primary Key property , and is working , but is is slow ( I'm thinking if Entity framework has a built in method to get this directly ??? )

      Public Function Get_FK(ctx As MyEntities, entity As Object, parenttable As String) As String
      Dim fk = entity.MetadataWorkspace.GetItems(Of Metadata.Edm.AssociationType)(Metadata.Edm.DataSpace.CSpace).Where(Function(a) a.IsForeignKey)
      Dim fkname = fk.Where(Function(x) x.ReferentialConstraints(0).ToRole.Name = parenttable)
      Dim refcol = fkname.Select(Function(x) x.ReferentialConstraints(0).FromProperties(0).Name).First()
      return refcol
      End Function

      This is for the foreign key (related with a parenttable that is passed as parameter ). This is not working. I get an error in the first line because entity is a proxy object

      D Offline
      D Offline
      dilkonika
      wrote on last edited by
      #2

      For the Primary key :

      Public Function Get_PK(ctx As MyEntities, entity As Object) As String
      Dim objectContext = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
      Dim t As Type = entity.GetType.BaseType
      Dim m As MethodInfo = objectContext.GetType().GetMethod("CreateObjectSet", New Type() {})
      Dim generic As MethodInfo = m.MakeGenericMethod(t)
      Dim st As Object = generic.Invoke(objectContext, Nothing)
      Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
      Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
      Dim keyNames As IEnumerable(Of String) = entitySet.ElementType.KeyMembers.[Select](Function(k) k.Name)
      Return keyNames(0)
      End Function

      For the Foreign key :

      Public Function Get_FK(ctx As MyEntities, entity As Object, parenttable As String) As String
      Dim objectCont = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
      Dim t As Type = entity.GetType.BaseType
      Dim m As MethodInfo = objcont.GetType().GetMethod("CreateObjectSet", New Type() {})
      Dim generic As MethodInfo = m.MakeGenericMethod(t)
      Dim st As Object = generic.Invoke(objcont, Nothing)
      Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
      Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
      Dim elementtype = entitySet.ElementType
      Dim entitymember = elementtype.NavigationProperties.Where(Function(t1) t1.ToEndMember.Name = parenttable)
      Return entitymember.First.GetDependentProperties.First.Name
      End Function

      Please give me some reputation , because I found the answer ;P

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups