积极答复者
在Entity Framework中,怎样得到主键名称、实体集名称?

问题
答案
-
// Get entity's key name
string GetKeyProperty(Type entityType)
{
foreach (var prop in entityType.GetProperties())
{
var attr = prop.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).FirstOrDefault()
as EdmScalarPropertyAttribute;
if (attr != null && attr.EntityKeyProperty)
return prop.Name;
}
return null;
}
Mog Liang
全部回复
-
// Get entity's key name
string GetKeyProperty(Type entityType)
{
foreach (var prop in entityType.GetProperties())
{
var attr = prop.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false).FirstOrDefault()
as EdmScalarPropertyAttribute;
if (attr != null && attr.EntityKeyProperty)
return prop.Name;
}
return null;
}
Mog Liang