如何从linq.table <tentity>获得列构曲目?[英] How do I get the ColumnAttributes from a Linq.Table<TEntity>?

本文是小编为大家收集整理的关于如何从linq.table <tentity>获得列构曲目?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从DataContext返回列的属性.

如何拔出columnattribute meta数据?

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;
}

如何访问用户对象或Table<User>对象以获取有关列的元数据(ISPRIMARYKEY,CANBENULL等)?

预先感谢.仍在学习...

推荐答案

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

从那里,您可以找到各种各样的东西.

本文地址:https://www.itbaoku.cn/post/1557073.html

问题描述

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

推荐答案

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.