要获取实体框架 4.0 上的数据库表名称,我这样做:
ObjectSetInstance.EntitySet.ToString()
有没有办法在 Entity Framework 4.1 上执行此操作?
扩展方法数据库上下文 and 对象上下文:
public static class ContextExtensions
{
public static string GetTableName<T>(this DbContext context) where T : class
{
ObjectContext objectContext = ((IObjectContextAdapter) context).ObjectContext;
return objectContext.GetTableName<T>();
}
public static string GetTableName<T>(this ObjectContext context) where T : class
{
string sql = context.CreateObjectSet<T>().ToTraceString();
Regex regex = new Regex("FROM (?<table>.*) AS");
Match match = regex.Match(sql);
string table = match.Groups["table"].Value;
return table;
}
}
使用 ObjectContext 对象:
ObjectContext context = ....;
string table = context.GetTableName<Foo>();
使用 DbContext 对象:
DbContext context = ....;
string table = context.GetTableName<Foo>();
更多信息请点击这里:
实体框架:从实体获取映射表名称 http://ruijarimba.wordpress.com/2012/03/18/entity-framework-get-mapped-table-name-from-an-entity/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)