How to get database table name with schema name from object programmatically

I am trying to get the SQL table name for an object. When I work with MetadataWorkspace queries, I get a lot of information from an object or storage space. But the schema name and table name are missing.

I am trying to query all EntityType objects for CSpace and SSpace, and I see that both of them are specified correctly, but I cannot figure out how to get SSpace from CSpace.

So to speak, I have a type in the User object model - how can I find the name of a table with the name of the schema (tkp.User) in the database?

Is there any way to do this?

+4
source share
1 answer

There is no perfect answer to this, but this should work ...

 var ssSpaceSet = objectContext.MetadataWorkspace .GetItems<EntityContainer>(DataSpace.SSpace).First() .BaseEntitySets .First(meta => meta.ElementType.Name == "YourEntityName"); 

If you look in the debugger, the Table property should have the name of the real table.

You need to use reflection in the last part.
The good news is transparent work with EF6 (since it has a similar base class)

(it is similar for schema , should also be there - this is space for Db / SQL display names, faces, etc.)

Look at my post with many details.
Get a model schema for programmatically creating a database using a provider that does not support CreateDatabase

<h / "> Get a model schema for programmatically creating a database using a provider that does not support CreateDatabase
How can I read EF DbContext metadata programmatically?
How to check unit test that property labels are calculated in the ORM model?
Convert program data to EF5 Code First migration
Entity Framework MigrationSqlGenerator for SQLite
http://entityframework.codeplex.com/
Entity Framework - get table name from object
ef code first: get entity table name without dataannotations
Get database table name from Entity Framework metadata

http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

+7
source

All Articles