Click here to Skip to main content
15,920,110 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: vb.net to vb6 convertion Pin
Dave Kreskowiak17-Apr-15 3:57
mveDave Kreskowiak17-Apr-15 3:57 
Questionvb6 phsical keyboard detect Pin
Member 1160234116-Apr-15 21:01
Member 1160234116-Apr-15 21:01 
AnswerRe: vb6 phsical keyboard detect Pin
Dave Kreskowiak17-Apr-15 2:28
mveDave Kreskowiak17-Apr-15 2:28 
GeneralRe: vb6 phsical keyboard detect Pin
Member 1160234117-Apr-15 16:17
Member 1160234117-Apr-15 16:17 
AnswerRe: vb6 phsical keyboard detect Pin
Sascha Lefèvre17-Apr-15 3:00
professionalSascha Lefèvre17-Apr-15 3:00 
GeneralRe: vb6 phsical keyboard detect Pin
Member 1160234117-Apr-15 16:25
Member 1160234117-Apr-15 16:25 
QuestionHow to get the item's data type in an bound Bindingsource that actually is empty? Pin
dilkonika16-Apr-15 19:45
dilkonika16-Apr-15 19:45 
QuestionGet the primary key property name and the Foreign key property name of an arbitrary entity Pin
dilkonika16-Apr-15 16:56
dilkonika16-Apr-15 16:56 
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 and MetaDataWorkspace is not a member of proxy types. and I don't know if this function works at all.

What can I do ?

Thank you !

modified 17-Apr-15 12:15pm.

AnswerRe: Get the primary key property name and the Foreign key property name of an arbitrary entity Pin
dilkonika19-Apr-15 4:56
dilkonika19-Apr-15 4:56 
Questionvb.net 2010 sum value in a collectionh Pin
dcof16-Apr-15 11:48
dcof16-Apr-15 11:48 
AnswerRe: vb.net 2010 sum value in a collectionh Pin
Sascha Lefèvre16-Apr-15 12:27
professionalSascha Lefèvre16-Apr-15 12:27 
GeneralRe: vb.net 2010 sum value in a collectionh Pin
dcof17-Apr-15 4:17
dcof17-Apr-15 4:17 
GeneralRe: vb.net 2010 sum value in a collectionh Pin
Sascha Lefèvre17-Apr-15 6:07
professionalSascha Lefèvre17-Apr-15 6:07 
QuestionCreate a list of Unknown type at compile time Pin
dilkonika16-Apr-15 10:58
dilkonika16-Apr-15 10:58 
AnswerRe: Create a list of Unknown type at compile time Pin
Sascha Lefèvre16-Apr-15 11:02
professionalSascha Lefèvre16-Apr-15 11:02 
GeneralRe: Create a list of Unknown type at compile time Pin
dilkonika16-Apr-15 11:53
dilkonika16-Apr-15 11:53 
GeneralRe: Create a list of Unknown type at compile time Pin
Sascha Lefèvre16-Apr-15 12:15
professionalSascha Lefèvre16-Apr-15 12:15 
GeneralRe: Create a list of Unknown type at compile time Pin
dilkonika16-Apr-15 13:05
dilkonika16-Apr-15 13:05 
GeneralRe: Create a list of Unknown type at compile time Pin
Sascha Lefèvre16-Apr-15 13:14
professionalSascha Lefèvre16-Apr-15 13:14 
GeneralRe: Create a list of Unknown type at compile time Pin
dilkonika16-Apr-15 13:51
dilkonika16-Apr-15 13:51 
GeneralRe: Create a list of Unknown type at compile time Pin
Sascha Lefèvre16-Apr-15 14:18
professionalSascha Lefèvre16-Apr-15 14:18 
GeneralRe: Create a list of Unknown type at compile time Pin
dilkonika16-Apr-15 14:23
dilkonika16-Apr-15 14:23 
GeneralRe: Create a list of Unknown type at compile time Pin
Sascha Lefèvre16-Apr-15 14:39
professionalSascha Lefèvre16-Apr-15 14:39 
GeneralRe: Create a list of Unknown type at compile time Pin
dilkonika16-Apr-15 16:03
dilkonika16-Apr-15 16:03 
GeneralRe: Create a list of Unknown type at compile time Pin
Sascha Lefèvre17-Apr-15 2:53
professionalSascha Lefèvre17-Apr-15 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.