Get the primary key property name and the Foreign key property name of an arbitrary entity
-
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 FunctionThis 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 FunctionThis 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
-
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 FunctionThis 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 FunctionThis 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
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 FunctionFor 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 FunctionPlease give me some reputation , because I found the answer ;P