1

I'm just learning to work with partial classes in VB.NET and VS2008. Specifically, I'm trying to extend a LINQ to SQL class that was automatically created by SqlMetal.

The automatically generated class looks like this:

Partial Public Class DataContext
    Inherits System.Data.Linq.DataContext

 ...


<Table(Name:="dbo.Concessions")>  _
Partial Public Class Concession

 ...

     <Column(Storage:="_Country", DbType:="Char(2)")>  _
   Public Property Country() As String
          ...
    End Property

 ...

End Class

In a separate file, here's what I'm trying to do:

Partial Public Class DataContext

    Partial Public Class Concession

        Public Function Foo() as String
            Return DoSomeProcessing(Me.Country)
        End Function

    End Class

End Class

... but I get blue jaggies under 'Me.Country' and the message 'Country' is not a member of 'DataContext.Concession'. Both halves of the partial class are in the same namespace.

So how do I access the properties of the automatically-generated half of the partial class, from my half of the partial class?

Kevin Fairchild
  • 10,891
  • 6
  • 33
  • 52
Herb Caudill
  • 50,043
  • 39
  • 124
  • 173

2 Answers2

3

Unless VB.NET generates different stuff in its LINQ to SQL files from C# the classes of the DB tables aren't within the DataContext class, just beside it.

So you have the class MyNamespace.DataContext.Concession when the other half of the partial class is realy MyNamespace.Concession

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
0

(This related to VB.NET - might be differences with C# projects)

I put my entities in their own namespace by configuring the Linq-to-SQL model property.

e.g. MyCo.MyProj.Business.Entities

I then add non-Linq business entities in there too, so they are all in the same namespace.

However, when trying to do the above partial class additions, I found that the partial class (i.e. the one you generate, not the auto-generated LINQ class) MUST be in the same project as the Linq-to-SQL model. Otherwise in the Class View and Object Viewer you see two separate classes - seemingly in the same namespace, but not really. Not sure if this is a bug or I am doing something wrong.

But, anyway, putting the partial class file in the same project as your model works.

Joe Niland
  • 885
  • 1
  • 14
  • 31