Extension methods for ObjectContext
and DbContent
to get the mapped table name from an entity.
Table of Contents
The Problem
I am working on a set of extension methods to perform a bulk insert using Entity Framework, using internally the SqlBulkCopy
object. One of the steps involved is to get the mapped table name from an entity. After some searching on Google, I found a post on StackOverflow that led me to the solution.
The Solution
The trick is to use the method ObjectQuery.ToTraceString
to generate a SQL Select
statement for an entity, and then extract the table name from that statement.
Let’s assume that you have an entity named Album
corresponding to a table named dbo.Albums
.
string sql = context.CreateObjectSet<T>().ToTraceString();
...
The generated SQL for that entity can be something like this:
SELECT
[Extent1].[AlbumId] AS [AlbumId],
[Extent1].[GenreId] AS [GenreId],
[Extent1].[ArtistId] AS [ArtistId],
[Extent1].[Title] AS [Title],
[Extent1].[Price] AS [Price],
[Extent1].[AlbumArtUrl] AS [AlbumArtUrl]
FROM [dbo].[Albums] AS [Extent1]
So, all we need to do is to parse the SELECT
statement to get the table name. This is the approach used in the post above, but it has some limitations – that code will work only for tables that are in the default SQL Server schema (dbo.{tableName}
). I made some changes to that code and I’m extracting the full table name using regular expressions.
The Extension Methods
I have created one extension method for DbContext and another 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>();
References
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Rome, Italy.
I have more than 10 years of experience developing software using the .NET Framework and other technologies (Web development, Databases, ...).
Some of my professional interests are: software development best practices, software architecture, cloud computing, Continuous Integration (CI), Continuous Delivery (CD) and agile methodologies such as Scrum, Kanban, Lean and any other methodology that can help me to become a better and more productive software engineer.
I believe in good code - code that is readable, maintainable, reusable, testable and deployable. This means that I'm not the "quick and dirty" type, I write code for the medium/long term whenever possible.
Something else about me - I love music, I am an amateur photographer, not a big fan of gyms (I prefer to do some outdoor activity such as walking/hiking), big foodie (I love Mediterranean cuisine and my glass of wine!).