2

My problem is that i have to import my entities from an existing DB. The DB has schemas, and some tables share the same names in different schemas. When this generates the model i get MyTable and MyTable1 (if that's what they were called).

What i'd like to do is name them MyTable if it uses the dbo schema, but if it's any other schema use the schema name as the prefix eg. MySchema_MyTable.

I know I can do this manually, by changing the name in the designer, but this might not be worked on solely by myself and more tables and schemas will be added to this database. So i want to enforce this naming convention.

I was thinking the best way would be to use t4 templates. I can easily change it to add a prefix and this works, but i don't know how to get the database schema name for that specific entity. T4 templates don't come with intellisense.

I would be grateful for your suggestions.

Thanks,

user1506481
  • 119
  • 2
  • 6
  • Your question is not clear to me. Do you want to write a T4 template that walks over all tables in a database, determines their "fully scoped" name including the schema name, and then generates code using those fully scoped names? Also, what database are you using? – Reinier Torenbeek Jul 06 '12 at 19:22
  • Yes, in a nutshell I want it to go through all the tables in the database and create a class representing each table. The name of these classes to be the "_". I was hoping this would be possible, the edmx files generated by entity framework contain schema names and table names. Thanks
    – user1506481 Jul 08 '12 at 16:42
  • Check out this great answer to [T4 template to Generate Enums](http://stackoverflow.com/a/4480173/1380680) and [this blog post](http://blog.sqlauthority.com/2009/06/17/sql-server-list-schema-name-and-table-name-for-database/). If you combine the two, you should be able to get what you are looking for in the case that you are using SQL Server, or adjust it for your particular DB. Does that help at all? – Reinier Torenbeek Jul 08 '12 at 18:51

1 Answers1

2

You can get to the database information using TiraggoEdmx. See http://brewdawg.github.io/Tiraggo.Edmx/ you can install it via NuGet within Visual Studio and it serves up all of the metadata from your EDMX files that Microsoft hides from you, very simple, works great.