VB 2005 • Map Databases to DLinq DataContext Partial Classes

Listing 1. LINQ's PDC 2005 technology preview bits don't provide a command-line code generator to create a partial entity class from database metadata. This code example is the VB 2005 port of a part of the C# DataContext class for SQL Server 2000's Northwind sample database from the Northwind.cs class file. Future LINQ releases probably will add codegen capabilities similar to those provided by Cω and integrate object-mapping code generation with Visual Studio.

Public Partial Class Northwind
        Inherits DataContext

        Public Categories As Table(Of Category)
        Public Customers As Table(Of Customer)
        Public Shippers As Table(Of Shipper)
        Public Suppliers As Table(Of Supplier)
        Public Orders As Table(Of Order)
        Public Products As Table(Of Product)
        ...

        Public Sub New()
        End Sub

        Public Sub New(ByVal connectionString As String)
                MyBase.New(connectionString)
        End Sub

        Public Sub New(ByVal connection _
                As System.Data.IDbConnection)
                MyBase.New(connection)
        End Sub
End Class

Public Partial Class Customer
        Implements System.Data.DLinq.IChangeNotifier

        Private _CustomerID As String
        Private _CompanyName As String
        ...
        <Column(Storage:="_CustomerID", DbType:= _
                "NChar(5) NOT NULL", Id:=True)> _
        Public Property CustomerID() As String
                Get
                        Return Me._CustomerID
                End Get
                Set (ByVal Value As String)
                        If Me._CustomerID <> Value Then
                                Me.OnChanging()
                                Me._CustomerID = Value
                        End If
                End Set
        End Property

        <Column(Storage:="_CompanyName", 
                DbType:="NVarChar(40) NOT NULL")> _
        Public Property CompanyName() As String
                Get
                        Return Me._CompanyName
                End Get
                Set (ByVal Value As String)
                        If Me._CompanyName <> Value Then
                                Me.OnChanging()
                                Me._CompanyName = Value
                        End If
                End Set
        End Property
        ...
End Class