数据库优先:获取任意实体的主键值和父属性值

时间:2015-04-16 20:07:32

标签: vb.net entity-framework

我正在使用Entity framework 6,Database First。和VB.net。

我的情况是:

  • 我有一个对象(这是一个实体对象,但该类只在运行时才知道)。
  • 我需要获取(并修改)主键的值和父字段的值(如果它相关)。

有可能得到这些吗?

谢谢!

更新 实际上我有2个函数应该返回主键属性的名称和外键属性:

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

这是针对主键属性,并且正在运行,但很慢(我想是否Entity框架有内置方法直接获取它?)

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

这是针对外键(与作为参数传递的父表相关)。 这不起作用。我在第一行收到错误,因为entity是代理对象而MetaDataWorkspace不是代理类型的成员。我根本不知道这个功能是否有效。

我该怎么办?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不知道是否有更好的解决方案,但这有效:

对于主键:

 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

我如何使用:

阅读值:

MessageBox.Show(CallByName(MyObject1, Get_PK(context, MyObject1), CallType.Get).ToString)

修改值:

CallByName(MyObject1, Get_PK(context, MyObject1), CallType.Let, 7)

.........................

对于父属性:

  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

我如何使用:

阅读值:

MessageBox.Show(CallByName(MyObject1, Get_FK(context, MyObject1,"Parentobj"), CallType.Get).ToString)

修改值:

CallByName(MyObject1, Get_FK(context, MyObject1,"Parentobj"), CallType.Let, 7)