1

I'm trying to return attributes of columns from my DataContext.

How can I pull out the ColumnAttribute meta data?

public class MyDataContext : DataContext
{
    public Table<User> User;
    public MyDataContext(string connection) : base(connection) { }
}

[Table(Name = "User")]
public class User
{
    [Column(IsPrimaryKey = true)]
    public long ID;
    [Column]
    public string FirstName;
    [Column(CanBeNull=false)]
    public string LastName;

    int VersionNumber = 1000;
}

How do I access the User object or Table<User> object to get the MetaData (IsPrimaryKey, CanBeNull, etc.) about the columns?

Thanks in advance. Still learning...

Jeremiah
  • 5,386
  • 9
  • 38
  • 45

1 Answers1

4
var context = new MyDataContext();
MetaTable userMeta = context.Mapping.GetTable(typeof(User));
var dataMembers = userMeta.RowType.PersistentDataMembers;

From there, you can get to all kinds of stuff.

mmdemirbas
  • 9,060
  • 5
  • 45
  • 53
Mike Two
  • 44,935
  • 9
  • 80
  • 96
  • 1
    foreach( MetaDataMember member in dataMembers ) { bool canBeNull = member.CanBeNull; } Thanks for your help. I just wanted to show how to use your method to access CanBeNull. – Jeremiah Aug 28 '09 at 14:32