views:

262

answers:

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...

+4  A: 

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

Mike Two
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