У меня есть класс, который добавляет дополнительную информацию о столбце для linq2sql (см. Код ниже)
прямо сейчас, я должен явно указать, для какого столбца я хочу эту информацию, как бы вы поместили этот код с циклом в каждый столбец в каждой таблице из файла dbml?
Я проводил тест на очень маленькой БД, теперь мне нужно внедрить его в гораздо большую базу данных, и я действительно не хочу делать это вручную для каждой таблицы / столбца
это займет несколько часов.
как это используется:
Partial Class Contact ''contact is a table inside a dbml file.
Private _ContactIDColumn As ExtraColumnInfo
Private _ContactNameColumn As ExtraColumnInfo
Private _ContactEmailColumn As ExtraColumnInfo
Private _ContactTypeIDColumn As ExtraColumnInfo
Private Sub OnCreated()
initColumnInfo()
End Sub
Private Sub initColumnInfo()
Dim prop As PropertyInfo
prop = Me.GetType.GetProperty("ContactID")
_ContactIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactName")
_ContactNameColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactEmail")
_ContactEmailColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactTypeID")
_ContactTypeIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Nothing
End Sub
Public ReadOnly Property ContactIDColumn() As ExtraColumnInfo
Get
Return _ContactIDColumn
End Get
End Property
Public ReadOnly Property ContactNameColumn() As ExtraColumnInfo
Get
Return _ContactNameColumn
End Get
End Property
Public ReadOnly Property ContactEmailColumn() As ExtraColumnInfo
Get
Return _ContactEmailColumn
End Get
End Property
Public ReadOnly Property ContactTypeIDColumn() As ExtraColumnInfo
Get
Return _ContactTypeIDColumn
End Get
End Property
End Class
Что такое ExtraColumnInfo:
Public Class ExtraColumnInfo
Private _column As ColumnAttribute
Private _MaxLength As Nullable(Of Integer)
Private _Precision As Nullable(Of Integer)
Private _Scale As Nullable(Of Integer)
Private _IsIdentity As Boolean
Private _IsUniqueIdentifier As Boolean
Sub New(ByVal column As ColumnAttribute)
Dim match As Match
_column = column
Match = Regex.Match(DBType, "^.*\((\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_MaxLength = If(match.Success, Convert.ToInt32(match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Precision = If(Match.Success, Convert.ToInt32(Match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Scale = If(match.Success, Convert.ToInt32(match.Groups(2).Value), Nothing)
match = Regex.Match(DBType, "^.*\bIDENTITY\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsIdentity = match.Success
match = Regex.Match(DBType, "^.*\bUniqueIdentifier\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsUniqueIdentifier = match.Success
End Sub
'others available information
'AutoSync
'Expression
'IsVersion
'Name
'Storage
'TypeId
'UpdateCheck
'maybe more
Public ReadOnly Property CanBeNull() As Boolean
Get
Return _column.CanBeNull
End Get
End Property
Public ReadOnly Property IsDbGenerated() As Boolean
Get
Return _column.IsDbGenerated
End Get
End Property
Public ReadOnly Property IsPrimaryKey() As Boolean
Get
Return _column.IsPrimaryKey
End Get
End Property
Public ReadOnly Property DBType() As String
Get
Return _column.DbType
End Get
End Property
Public ReadOnly Property MaxLength() As System.Nullable(Of Integer)
Get
Return _MaxLength
End Get
End Property
Public ReadOnly Property Precision() As System.Nullable(Of Integer)
Get
Return _Precision
End Get
End Property
Public ReadOnly Property Scale() As System.Nullable(Of Integer)
Get
Return _Scale
End Get
End Property
Public ReadOnly Property IsIdentity() As Boolean
Get
Return _IsIdentity
End Get
End Property
Public ReadOnly Property IsUniqueIdentifier() As Boolean
Get
Return _IsUniqueIdentifier
End Get
End Property
End Class