在Entity Framework中,怎样得到主键名称,实体集名称

 我来答
Oe53fo7
2017-05-06 · TA获得超过261个赞
知道答主
回答量:302
采纳率:0%
帮助的人:102万
展开全部
try this by using LINQ-TO-OBJECT to find out all the properties, and then use GetCustomAttributes to find out which fits well.
通过GetProperties()方法获取列名list,即linq to sql 语句获取;
namespace nibian
{
/// <summary>
/// Suppose this is a class that has two "Required" attributes
/// </summary>
public class Model
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public double Score { get; set; }
}

public class MainTest
{
static void Main(string[] args)
{
// This will search for each Attributes applied onto the property
// to find out whether "Required" exists or not.
var properties = typeof(Model).GetProperties().Where(p => p.GetCustomAttributes(typeof(RequiredAttribute), false).Length==1).Select(p=>p);

// This will output the Name of the assigned public properties.
foreach (var item in properties)
{
Console.WriteLine(item.Name);
}

}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式