Tod*_*ler 2 c# entity-framework columnattribute
我正在使用Entity Framework,这是其中一张表的元数据的样子。
[Column("pp_account")]
public string Account { get; set; }
[Column("pp_added")]
public DateTime? AddDate { get; set; }
[Column("pp_auser")]
public string AddUpdateUserName { get; set; }
[Column("pp_cfalt")]
public string AlternateForwardingNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
我对获取Column属性名称而不是公共名称感兴趣,即显示pp_added而不是“ AddDate”。到目前为止,这是我编写的一个foreach循环,几乎可以将我带到那里
foreach(var field in _personal.GetType().GetProperties())
{
sb.Append("\nName : " + field.Name + "\n");
sb.Append("Type : " + field.PropertyType.Name + "\n");
sb.Append("Attributes: " + field.Attributes + "\n");
sb.Append("Value : " + field.GetValue(_personal, null) + "\n");
}
Run Code Online (Sandbox Code Playgroud)
这是返回的内容:
Name: AddDate
Type: Nullable'1
Attributes: None
Value: 5/2/2014 12:00:00 AM
Run Code Online (Sandbox Code Playgroud)
我想得到的是
Name: pp_added
Type: Date
Value: 5/2/2014 12:00:00 AM
Run Code Online (Sandbox Code Playgroud)
我如何[Column("pp_added")]才能获得pp_added公众而不是公众Name?
这应该工作:
foreach(var field in _personal.GetType().GetProperties())
{
sb.Append("\nName : " + field.GetCustomAttribute<ColumnAttribute>().Name + "\n");
sb.Append("Type : " + field.PropertyType.Name + "\n");
sb.Append("Value : " + field.GetValue(_personal, null) + "\n");
}
Run Code Online (Sandbox Code Playgroud)
该ColumnAttribute是在System.ComponentModel.DataAnnotations.Schema命名空间。
| 归档时间: |
|
| 查看次数: |
1872 次 |
| 最近记录: |