【问题标题】:Map a dataset to a custom .NET object in ASP.NET将数据集映射到 ASP.NET 中的自定义 .NET 对象
【发布时间】:2012-05-24 00:07:24
【问题描述】:

我想将数据表中的数据映射到自定义 .NET 对象。假设我有一个 .NET 对象

Public Class MyClass
    Public ID
    Public Prop1
    Public Prop2
End Class

以及数据库中对应的表,其中包含列ID、Prop1和Prop2。

我想从数据库中生成一个 .NET 对象列表。目前我填充数据集并单独映射每个属性。有什么办法可以自动映射数据库,所以属性会根据属性/列名进行映射。

'retreive dataset from db'
DaAdapter.Fill(ds)

'create a new list of my object'
Private l As New List(Of MyClass)

'iterate through dataset (this is the step I want to get rid of - of course'
'it could only work if the property of the MyClass and the column in '
'the database have the same name)'
For Each r As DataRow in ds.Tables(0).Rows
    Dim itm As New MyClass
    itm.ID = r("ID")
    itm.Prop1 = r("Prop1")
    itm.Prop2 = r("Prop2")
    l.Add(itm)
Next

【问题讨论】:

    标签: asp.net database object datatable mapping


    【解决方案1】:

    我建议Entity Framework

    您可以使用database first 方法,只需指向您的数据库,它就会为您创建实体和关联。

    它真的很困难,我建议尝试一下

    【讨论】:

      【解决方案2】:

      没有直接的方法,尽管有许多工具可用于创建与数据库表完全相似的类,称为实体类。更多详情请查看以下链接

      Database tables to C# entity classes - generator for FluentNHibernate?

      您还可以使用 typeddataset、codesmith 工具。

      【讨论】:

        【解决方案3】:

        恕我直言。最简单的方法是使用Linq2Sql

        【讨论】:

          【解决方案4】:

          您可以使用某种对象关系映射 (ORM) 框架。 Entity Framework 就是其中之一

          【讨论】:

            猜你喜欢
            • 2011-04-24
            • 2018-10-31
            • 1970-01-01
            • 2020-10-01
            • 2018-05-14
            • 2021-11-07
            • 1970-01-01
            • 1970-01-01
            • 2020-08-17
            相关资源
            最近更新 更多