Attributes of linq autogenerated classes RRS feed

  • Question

  • Hi
    I have a class ( autogenerated using the linq to sql ) with a number of methods like below which have attributes, I am trying to read these attributes. ( to help add to the business logice layer to limit field size for strings check if nullable etc )

    Now I would love to be able to do something like below,
    help would be really appreciated.

    Dim x As New Client
    Dim Attribs = GetMyAttributes(  x.AddressLine1 ) ' I don't want to use strings as the property name

     <Column(Storage:="_AddressLine1", DbType:="NVarChar(100)",
     UpdateCheck:=UpdateCheck.Never), _
         DataMember(Order:=13)> _
        Public Property AddressLine1() As String
                Return Me._AddressLine1
            End Get
            Set(ByVal value As String)
                If (String.Equals(Me._AddressLine1, value) = False) Then
                    Me._AddressLine1 = value
                End If
            End Set
        End Property
    Friday, June 5, 2009 8:13 AM

All replies

  • If you want to read the L2S attributes, use the functionality in System.Data.Linq.Mapping.


    SomeDataContext dc = new SomeDataContext();
    //iterate through the collection of tables defined in the datacontext
    foreach (System.Data.Linq.Mapping.MetaTable table in dc.Mapping.GetTables())
        //iterate through the members
        foreach (System.Data.Linq.Mapping.MetaDataMember member in table.RowType.DataMembers)
            // stuff...

    Kristofer - Huagati Systems Co., Ltd. - - Cool tools for Linq-to-SQL and Entity Framework:
    Friday, June 5, 2009 9:26 AM
  • Ok

    thanks for this. In my database I have many tables ( and hence classess ) and I would like be able do

    dim c as New Client

    GetSomeAttributes ( c.Address ) or
    GetSomeAttributes ( c.Phone ) or


    dim u as New User
    GetSomeAttributes ( u.Name )

    GetSomeAttributes ( c.Address ) would return
    Column(Storage:="_AddressLine1", DbType:="NVarChar(100)",
     UpdateCheck:=UpdateCheck.Never), _
         DataMember(Order:=13)> _

    And I am unsure how to read the attributes for a property c.Address or c.Phone

    Friday, June 5, 2009 9:51 AM
  • Dim c As New Employee
    Dim mem As System.Data.Linq.Mapping.MetaDataMember _
        = dc.Mapping.GetTable(c.GetType()).RowType.DataMembers.Where(Function(m) m.Name = "Title").Single()
    System.Diagnostics.Debug.WriteLine(mem.Name + ", " + mem.Type.Name + ", " + mem.MappedName + ", " + mem.DbType)

    Kristofer - Huagati Systems Co., Ltd. - - Cool tools for Linq-to-SQL and Entity Framework:
    Friday, June 5, 2009 10:40 AM
  • Hi

    Like the answer using a linq query, but again it tests the method name using the string "Title" I would prefer using c.Title as database column names can change

    Sunday, June 7, 2009 11:10 AM