Asp.net 非类型化为强类型数据表

Asp.net 非类型化为强类型数据表,asp.net,datatable,strongly-typed-dataset,Asp.net,Datatable,Strongly Typed Dataset,我今天试着做些实验。我有一个使用非类型化数据表作为模型实体的应用程序。 它们都是这样制作的: Imports System.Data Imports System.Runtime.Serialization Imports System.ComponentModel <DesignerCategory("Code"), system.Serializable()> Partial Public Class SomeTable1 Inherits DataTable #Re

我今天试着做些实验。我有一个使用非类型化数据表作为模型实体的应用程序。 它们都是这样制作的:

Imports System.Data
Imports System.Runtime.Serialization
Imports System.ComponentModel

<DesignerCategory("Code"), system.Serializable()>
Partial Public Class SomeTable1
    Inherits DataTable

#Region
    Public Const TABLE_NAME As String = "SomeTable1"

    Public Const FIELD_SomeField1 As String = "SomeField1"
    Public Const FIELD_SomeField2 As String = "SomeField2"
#End Region

    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
    End Sub

    Public Sub New()
        MyBase.New()

        With Columns
            .Add(FIELD_SomeField1, GetType(System.String)).DefaultValue = String.Empty
            .Add(FIELD_SomeField2, GetType(System.Double)).DefaultValue = 0
        End With

        Dim keys(1) As DataColumn
        keys(0) = Columns(FIELD_SomeField1)
        TableName = TABLE_NAME
        PrimaryKey = keys
    End Sub
End Class
导入系统数据
导入System.Runtime.Serialization
导入System.ComponentModel
部分公共类表1
继承数据表
#区域
Public Const TABLE_NAME为String=“SomeTable1”
公共常量字段\u SomeField1为String=“SomeField1”
公共常量字段\u SomeField2为String=“SomeField2”
#末端区域
受保护的子新建(ByVal信息作为SerializationInfo,ByVal上下文作为StreamingContext)
MyBase.New(信息、上下文)
端接头
公共分新()
MyBase.New()
带柱
.Add(FIELD_SomeField1,GetType(System.String)).DefaultValue=String.Empty
.Add(FIELD_SomeField2,GetType(System.Double)).DefaultValue=0
以
Dim键(1)作为数据列
键(0)=列(字段\字段1)
TableName=表名称
PrimaryKey=密钥
端接头
末级
我目前正在与EF合作,所以在我的razzle中,我写了如下内容(是的,是vb):

部分公共类SomeTable1
继承数据表
好友属性SomePK1作为数据列
Friend属性SomeField1作为DataColumn
Friend属性SomeField2作为DataColumn
...
受保护的子新建(ByVal信息作为SerializationInfo,ByVal上下文作为StreamingContext)
MyBase.New(信息、上下文)
端接头
公共分新()
MyBase.New()
SomeField2=日期。现在
端接头
末级
我梦想着做一些与以前的dt相当的东西,并与当前的数据引擎完全兼容

然后类型转换错误(系统日期到数据列)打破了我的希望。我必须承认这是一个艰难的周末:)

所以在我完全放弃更改之前,是否有任何方法可以编写一个类型化的数据表,这样它就等同于上面的代码,但有一些新的优点? 这是一种非常古老的编程方式,我在网上找不到任何东西。
提前感谢。

我不确定是否完全理解,但看起来您将FIELD\u SomeField2定义为双精度字段

(第一段中的这一行)

但我看到您在第二个代码片段中将SomeField2定义为日期时间

<Required()>
<DataType(DataType.DateTime)>
Friend Property SomeField2 As DataColumn

Friend属性SomeField2作为DataColumn

所以可能只是类型不匹配…

我找到了如何做我想做的事。也许需要一些工作,但它是有效的。 我知道这是一种过时的做事方式,所以我在这里发帖是为了让像我这样被迫维护旧程序的人受益

执行类型化数据表的模板如下所示:

Imports System.Data
Imports System.ComponentModel
Imports System.Runtime.Serialization
Imports System.Diagnostics

'''<summary>
'''Represents the strongly named DataTable class.
'''</summary>
<Global.System.Serializable(), _
 Global.System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedTableSchema")> _
Partial Public Class tblMyTable
    Inherits TypedTableBase(Of tblMyTableRow)

    'Those are the StoredProcs names for (MANUAL) CRUD operations that the DBContext wrapper uses. (yuck! I hate thousands of them)
    'Public Const COMMAND_SAVE As String = "sp_MyTable_Save"
    'Public Const COMMAND_DELETE As String = "sp_MyTable_Delete"
    'Public Const COMMAND_LOADBY_ID As String = "sp_MyTable_LoadBy_Id"

    'Those are constants I maintain for untyped (but somewhat strong) compatibility
    Public Const FIELD_pID As String = "pID"
    Public Const FIELD_SomeOther As String = "SomeOtherField"

    'Basic CRUD, uses company data as the app hot swapps DBs (one for company)
    'Public Function Save(ByVal company As DataRow) As Short
    '    Return New Base(company).Update(Me, COMMAND_SAVE, COMMAND_DELETE)
    'End Function

    'Public Sub LoadByID(ByVal company As DataRow, Id As Integer)
    '    Me.Rows.Clear()
    '    Me.Merge(New Base(company).FillDataTable(Of tblMyTable)(COMMAND_LOADBY_ID, Id))
    'End Sub

    <DebuggerNonUserCodeAttribute()>
    Private Sub InitClass()
        Me.columnpID = New DataColumn(FIELD_pID, GetType(Integer), Nothing, MappingType.Element) With
                   {.AllowDBNull = False, .ReadOnly = True, .Unique = True,
                    .AutoIncrement = True, .AutoIncrementSeed = -1, .AutoIncrementStep = -1}
        MyBase.Columns.Add(Me.columnpID)
        Me.columnSomeOtherField = New DataColumn(FIELD_SomeOther, GetType(String), Nothing, MappingType.Element) With
                           {.MaxLength = 5, .AllowDBNull = False, .DefaultValue = String.Empty}
        MyBase.Columns.Add(Me.columnSomeOtherField)
    End Sub

    Private columnpID As DataColumn
    Private columnSomeOtherField As DataColumn

    <DebuggerNonUserCodeAttribute()>
    Public Sub New()
        MyBase.New()
        Me.TableName = "tblMyTable"
        Me.BeginInit()
        Me.InitClass()
        Me.EndInit()
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Friend Sub New(ByVal table As DataTable)
        MyBase.New()
        Me.TableName = table.TableName
        If (table.CaseSensitive <> table.DataSet.CaseSensitive) Then
            Me.CaseSensitive = table.CaseSensitive
        End If
        If (table.Locale.ToString <> table.DataSet.Locale.ToString) Then
            Me.Locale = table.Locale
        End If
        If (table.Namespace <> table.DataSet.Namespace) Then
            Me.Namespace = table.Namespace
        End If
        Me.Prefix = table.Prefix
        Me.MinimumCapacity = table.MinimumCapacity
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Protected Sub New(ByVal info As Global.System.Runtime.Serialization.SerializationInfo, ByVal context As Global.System.Runtime.Serialization.StreamingContext)
        MyBase.New(info, context)
        Me.InitVars()
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public ReadOnly Property pIDColumn() As DataColumn
        Get
            Return Me.columnpID
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public ReadOnly Property SomeOtherFieldColumn() As DataColumn
        Get
            Return Me.columnSomeOtherField
        End Get
    End Property

    <DebuggerNonUserCodeAttribute(), Browsable(False)>
    Public ReadOnly Property Count() As Integer
        Get
            Return Me.Rows.Count
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Default Public ReadOnly Property Item(ByVal index As Integer) As tblMyTableRow
        Get
            Return CType(Me.Rows(index), tblMyTableRow)
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public Overrides Function Clone() As DataTable
        Dim cln As tblMyTable = CType(MyBase.Clone, tblMyTable)
        cln.InitVars()
        Return cln
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function CreateInstance() As DataTable
        Return New tblMyTable()
    End Function

    <DebuggerNonUserCodeAttribute()>
    Friend Sub InitVars()
        Me.columnpID = MyBase.Columns(FIELD_pID)
        Me.columnSomeOtherField = MyBase.Columns(FIELD_SomeOther)
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public Function NewtblMyTableRow() As tblMyTableRow
        Return CType(Me.NewRow, tblMyTableRow)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function NewRowFromBuilder(ByVal builder As DataRowBuilder) As DataRow
        Return New tblMyTableRow(builder)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function GetRowType() As Global.System.Type
        Return GetType(tblMyTableRow)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Public Sub RemovetblMyTableRow(ByVal row As tblMyTableRow)
        Me.Rows.Remove(row)
    End Sub

End Class

'''<summary>
'''Represents strongly named DataRow class.
'''</summary>
Partial Public Class tblMyTableRow
    Inherits DataRow

    Private tabletblMyTable As tblMyTable

    <DebuggerNonUserCodeAttribute()>
    Friend Sub New(ByVal rb As DataRowBuilder)
        MyBase.New(rb)
        Me.tabletblMyTable = CType(Me.Table, tblMyTable)
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public Property pID() As Integer
        Get
            Return CType(Me(Me.tabletblMyTable.pIDColumn), Integer)
        End Get
        Set(value As Integer)
            Me(Me.tabletblMyTable.pIDColumn) = value
        End Set
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public Property SomeOtherField() As String
        Get
            Return CType(Me(Me.tabletblMyTable.SomeOtherFieldColumn), String)
        End Get
        Set(value As String)
            Me(Me.tabletblMyTable.SomeOtherFieldColumn) = value
        End Set
    End Property
End Class
导入系统数据
导入System.ComponentModel
导入System.Runtime.Serialization
导入系统。诊断
'''
''表示强命名的DataTable类。
'''
_
部分公共类tblMyTable
继承TypedTableBase(属于tblMyTableRow)
'这些是DBContext包装器使用的(手动)CRUD操作的StoredProcs名称。(恶心!我讨厌成千上万的人)
'Public Const COMMAND_SAVE As String=“sp_MyTable_SAVE”
'Public Const COMMAND_DELETE As String=“sp_MyTable_DELETE”
'Public Const COMMAND\u LOADBY\u ID As String=“sp\u MyTable\u LOADBY\u ID”
“这些是我为实现非类型化(但有点强)兼容性而维护的常量
公用常量字段\u pID为String=“pID”
Public Const FIELD_SomeOther As String=“SomeOtherField”
“基本CRUD,使用公司数据作为应用程序热交换DBs(一个用于公司)
'公共函数保存(ByVal公司作为数据行)为短
'返回新基地(公司)。更新(Me、命令保存、命令删除)
'结束函数
'公共子加载BYID(ByVal公司作为数据行,Id作为整数)
“我。行。清除()
'Me.Merge(新基(公司).FillDataTable(属于tblMyTable)(命令\u LOADBY\u ID,ID))
'末端接头
私有子类()
Me.columnpID=带有
{.AllowDBNull=False、.ReadOnly=True、.Unique=True、,
.AutoIncrement=True,.AutoIncrementSeed=-1,.AutoIncrementStep=-1}
MyBase.Columns.Add(Me.columnpID)
Me.columnsometherfield=newdatacolumn(FIELD_somether,GetType(String),Nothing,MappingType.Element)与
{.MaxLength=5、.AllowDBNull=False、.DefaultValue=String.Empty}
MyBase.Columns.Add(Me.columnsometherfield)
端接头
作为数据列的私有列PID
Private ColumnSometherField作为DataColumn
公共分新()
MyBase.New()
Me.TableName=“tblMyTable”
Me.BeginInit()
Me.InitClass()
Me.EndInit()
端接头
Friend Sub New(ByVal表作为DataTable)
MyBase.New()
Me.TableName=table.TableName
如果(table.CaseSensitive table.DataSet.CaseSensitive),则
Me.CaseSensitive=table.CaseSensitive
如果结束
如果(table.Locale.ToString table.DataSet.Locale.ToString),则
Me.Locale=table.Locale
如果结束
如果是(table.Namespace table.DataSet.Namespace),则
Me.Namespace=table.Namespace
如果结束
Me.Prefix=table.Prefix
Me.MinimumCapacity=表1.MinimumCapacity
端接头
受保护的子新建(ByVal信息为Global.System.Runtime.Serialization.SerializationInfo,ByVal上下文为Global.System.Runtime.Serialization.StreamingContext)
MyBase.New(信息、上下文)
Me.InitVars()
端接头
作为DataColumn的公共只读属性pIDColumn()
得到
还给我
结束
端属性
作为DataColumn的公共只读属性SomeOtherFieldColumn()
得到
返回我。其他字段
结束
端属性
公共只读属性计数()为整数
<Required()>
<DataType(DataType.DateTime)>
Friend Property SomeField2 As DataColumn
Imports System.Data
Imports System.ComponentModel
Imports System.Runtime.Serialization
Imports System.Diagnostics

'''<summary>
'''Represents the strongly named DataTable class.
'''</summary>
<Global.System.Serializable(), _
 Global.System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedTableSchema")> _
Partial Public Class tblMyTable
    Inherits TypedTableBase(Of tblMyTableRow)

    'Those are the StoredProcs names for (MANUAL) CRUD operations that the DBContext wrapper uses. (yuck! I hate thousands of them)
    'Public Const COMMAND_SAVE As String = "sp_MyTable_Save"
    'Public Const COMMAND_DELETE As String = "sp_MyTable_Delete"
    'Public Const COMMAND_LOADBY_ID As String = "sp_MyTable_LoadBy_Id"

    'Those are constants I maintain for untyped (but somewhat strong) compatibility
    Public Const FIELD_pID As String = "pID"
    Public Const FIELD_SomeOther As String = "SomeOtherField"

    'Basic CRUD, uses company data as the app hot swapps DBs (one for company)
    'Public Function Save(ByVal company As DataRow) As Short
    '    Return New Base(company).Update(Me, COMMAND_SAVE, COMMAND_DELETE)
    'End Function

    'Public Sub LoadByID(ByVal company As DataRow, Id As Integer)
    '    Me.Rows.Clear()
    '    Me.Merge(New Base(company).FillDataTable(Of tblMyTable)(COMMAND_LOADBY_ID, Id))
    'End Sub

    <DebuggerNonUserCodeAttribute()>
    Private Sub InitClass()
        Me.columnpID = New DataColumn(FIELD_pID, GetType(Integer), Nothing, MappingType.Element) With
                   {.AllowDBNull = False, .ReadOnly = True, .Unique = True,
                    .AutoIncrement = True, .AutoIncrementSeed = -1, .AutoIncrementStep = -1}
        MyBase.Columns.Add(Me.columnpID)
        Me.columnSomeOtherField = New DataColumn(FIELD_SomeOther, GetType(String), Nothing, MappingType.Element) With
                           {.MaxLength = 5, .AllowDBNull = False, .DefaultValue = String.Empty}
        MyBase.Columns.Add(Me.columnSomeOtherField)
    End Sub

    Private columnpID As DataColumn
    Private columnSomeOtherField As DataColumn

    <DebuggerNonUserCodeAttribute()>
    Public Sub New()
        MyBase.New()
        Me.TableName = "tblMyTable"
        Me.BeginInit()
        Me.InitClass()
        Me.EndInit()
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Friend Sub New(ByVal table As DataTable)
        MyBase.New()
        Me.TableName = table.TableName
        If (table.CaseSensitive <> table.DataSet.CaseSensitive) Then
            Me.CaseSensitive = table.CaseSensitive
        End If
        If (table.Locale.ToString <> table.DataSet.Locale.ToString) Then
            Me.Locale = table.Locale
        End If
        If (table.Namespace <> table.DataSet.Namespace) Then
            Me.Namespace = table.Namespace
        End If
        Me.Prefix = table.Prefix
        Me.MinimumCapacity = table.MinimumCapacity
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Protected Sub New(ByVal info As Global.System.Runtime.Serialization.SerializationInfo, ByVal context As Global.System.Runtime.Serialization.StreamingContext)
        MyBase.New(info, context)
        Me.InitVars()
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public ReadOnly Property pIDColumn() As DataColumn
        Get
            Return Me.columnpID
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public ReadOnly Property SomeOtherFieldColumn() As DataColumn
        Get
            Return Me.columnSomeOtherField
        End Get
    End Property

    <DebuggerNonUserCodeAttribute(), Browsable(False)>
    Public ReadOnly Property Count() As Integer
        Get
            Return Me.Rows.Count
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Default Public ReadOnly Property Item(ByVal index As Integer) As tblMyTableRow
        Get
            Return CType(Me.Rows(index), tblMyTableRow)
        End Get
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public Overrides Function Clone() As DataTable
        Dim cln As tblMyTable = CType(MyBase.Clone, tblMyTable)
        cln.InitVars()
        Return cln
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function CreateInstance() As DataTable
        Return New tblMyTable()
    End Function

    <DebuggerNonUserCodeAttribute()>
    Friend Sub InitVars()
        Me.columnpID = MyBase.Columns(FIELD_pID)
        Me.columnSomeOtherField = MyBase.Columns(FIELD_SomeOther)
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public Function NewtblMyTableRow() As tblMyTableRow
        Return CType(Me.NewRow, tblMyTableRow)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function NewRowFromBuilder(ByVal builder As DataRowBuilder) As DataRow
        Return New tblMyTableRow(builder)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Protected Overrides Function GetRowType() As Global.System.Type
        Return GetType(tblMyTableRow)
    End Function

    <DebuggerNonUserCodeAttribute()>
    Public Sub RemovetblMyTableRow(ByVal row As tblMyTableRow)
        Me.Rows.Remove(row)
    End Sub

End Class

'''<summary>
'''Represents strongly named DataRow class.
'''</summary>
Partial Public Class tblMyTableRow
    Inherits DataRow

    Private tabletblMyTable As tblMyTable

    <DebuggerNonUserCodeAttribute()>
    Friend Sub New(ByVal rb As DataRowBuilder)
        MyBase.New(rb)
        Me.tabletblMyTable = CType(Me.Table, tblMyTable)
    End Sub

    <DebuggerNonUserCodeAttribute()>
    Public Property pID() As Integer
        Get
            Return CType(Me(Me.tabletblMyTable.pIDColumn), Integer)
        End Get
        Set(value As Integer)
            Me(Me.tabletblMyTable.pIDColumn) = value
        End Set
    End Property

    <DebuggerNonUserCodeAttribute()>
    Public Property SomeOtherField() As String
        Get
            Return CType(Me(Me.tabletblMyTable.SomeOtherFieldColumn), String)
        End Get
        Set(value As String)
            Me(Me.tabletblMyTable.SomeOtherFieldColumn) = value
        End Set
    End Property
End Class