The extension methods I have created one extension method for DbContext and other for ObjectContext: 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; } } Using the code Getting the mapped table name for an entity named Album, using a ObjectContext object: ObjectContext context = ....; string table = context.GetTableName<Album>(); Or using a DbContext object: DbContext context = ....; string table = context.GetTableName<Album>();
原文 Entity Framework: Get mapped table name from an entity