即使我修改了实体的名称,如何在 EF 中获取实体的真实表名。我想扩展一个批量删除功能。</p>
问问题
92 次
1 回答
0
我认为这样的事情应该有效:
string GetTableName()
{
Type t = this.GetType();
var tableAttributes = t.GetCustomAttributes(typeof(TableAttribute), true);
if (tableAttributes.Length == 0)
return t.Name;
else
return ((TableAttribute)tableAttributes[0]).Name;
}
基本上,如果该类被标记为TableAttribute,则可以从中获取真实的表名。如果未标记,则 EF 的默认设置是使用与类相同的名称。
于 2012-05-17T12:56:51.970 回答