Skip to content

Instantly share code, notes, and snippets.

@mvalipour
Created August 8, 2016 15:46
Show Gist options
  • Select an option

  • Save mvalipour/4e103716cfcaecff92feb213723366fb to your computer and use it in GitHub Desktop.

Select an option

Save mvalipour/4e103716cfcaecff92feb213723366fb to your computer and use it in GitHub Desktop.
Get Table name from type in Entity Framework
public static string GetTableName(this ObjectContext context, Type t)
{
var entityName = t.Name;
var storageMetadata = context.MetadataWorkspace.GetItems<EntityContainerMapping>(DataSpace.CSSpace);
foreach (var ecm in storageMetadata)
{
EntitySet entitySet;
if (ecm.StoreEntityContainer.TryGetEntitySetByName(entityName, true, out entitySet))
{
return $"{entitySet.Schema}.[{entitySet.Table}]";
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment