How do I get the ColumnAttributes from a Linq.Tabl

2019-08-12 08:19发布

问题:

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

回答1:

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.