在Entity Framework中,怎样得到主键名称,实体集名称
1个回答
展开全部
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);
}
}
}
}
通过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);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询