I'm trying to figure out a way to get the underlying SQL table name for a given entity type. I've experimented around with the MetadataWorkspace queries and while I can get lots of information from the object or the storage space, I can't seem to figure out how to map between the two.
So say I have a type in the object model called Lookup - how do I find the tablename (wws_lookups) in the database?
I can query all the EntityType objects for CSpace and SSpace and I can see both listed correctly but I can't figure out how to get SSpace from CSpace.
Is there any way to do this?
For EF6, mixing/compressing code from other answers here and around (VB, I'm sorry):
It works for db-first.
Relatively easy to understand following an .edmx file.
Adding another answer for the special case when you use annotations to explicitly tell EF which table name to use. For example, if you have:
You can easily access the
TableAttribute
annotation to find the table name:which is
tblCompany
for the given sample.As ready-to-use method:
(I am aware that this will not help the OP but given the title of the question, people may end up here who may be looking for this answer.)
If you're doing codefirst in EF6, you can just add something like the following to your dbcontext class.
Here is a version assuming you have context and have a selected entity in memory that you need to find the real table name for.
EDIT This answer now obsolete due to new feature in EF 6.1 : mapping between table types. Go there first!
I had a problem with the other answers because I have a derived type. I got this method (inside my context class) to work - I have only one layer of inheritance in my model at the moment
NB There are plans to improve the Metadata API and if this isn't getting what we want then we can look at EF Code First Mapping Between Types & Tables
Here is another way to find the table name. It is a bit odd but works. VB: