How do I get the ColumnAttributes from a Linq.Table<TEntity>?

我是研究僧i 提交于 2020-01-25 00:06:22

问题


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.



来源:https://stackoverflow.com/questions/1347209/how-do-i-get-the-columnattributes-from-a-linq-tabletentity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!