【问题标题】:Retrieving Database Schema Linq to SQL VB.Net检索数据库架构 Linq 到 SQL VB.Net
【发布时间】:2013-01-31 13:21:15
【问题描述】:

我想检索每个列名和数据类型,然后检查这些列是否是外键,然后查询它们的键表关系!!!可以做吗??我用谷歌搜索了 3 天,我知道我必须使用映射模型或反射或两者,,,,但我做不到。

我将简化我需要的假设: TABLE1 具有外键(COL3) 引用 TABLE0 中的主键(COL1):

  1. iterate TABLE1 Columns 检查每个列是否为外键(同时获取其数据类型)

  2. 获取关系以确定关联表(TABLE0)

  3. 检索主键表(TABLE0)

【问题讨论】:

  • 你到底想达到什么目的?你想加入数据吗?或者只是知道相关表是什么? (例如,要获取该数据,以显示下拉列表或其他内容。)尝试编辑您的问题,不要使用许多感叹号和问号。这听起来不太好听,而且听起来非常大喊大叫和绝望。
  • 是的,我需要知道相关表是什么。然后我想检索相关表的数据。

标签: vb.net linq linq-to-sql mapping schema


【解决方案1】:

我明白了 我做了一个函数,返回每个外键的类型和相关的表类类型

Private Function GetForeignKeyTables(ByVal myTableType As Type) As List(Of myForeignKeys)

    Dim myDx = New Tester.DataClasses1DataContext
    Dim mymodel As New AttributeMappingSource

    Dim myAsociations = mymodel.GetModel(GetType(DataClasses1DataContext)).GetTable(myTableType).RowType.Associations

    Dim asc = From m In myAsociations Where m.IsForeignKey
               Select New myForeignKeys With {.KeyDataType = m.ThisKey.First.DbType, .RelatedTableType = m.OtherType}

    Return asc.ToList

End Function

Private Class myForeignKeys
    Property KeyDataType As String
    Property RelatedTableType As MetaType

End Class 

但我仍然需要从这些相关表中检索数据。

我的意思是如何从它的 MetaType 变量创建一个类的实例?

【讨论】:

  • [已解决] 我可以使用 datacontext gettable 方法获取表格。暗表 = myDx.GetTable(.GetType)
猜你喜欢
  • 2011-11-11
  • 1970-01-01
  • 2014-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多