2

I've searched but haven't been able to find an answer to this question. Currently our Db users prefixes of tables - e.g. tblUsers. I've updated the EF templates to remove the "tbl" from the generated class names. However I still can't figure out how to change the output file name to match.

Is it possible or am I asking for the moon? I’m using EF Power Tools Beta 3 in VS 2012. Any help would be GREATLY appreciated!

Patrick
  • 21
  • 1
  • 5
  • +1 I want to do this too. I'm trying to modify the templates to remove the "tbl", but it's quite difficult to get all the places. How did you do it? Added bounty too.. – Colin Mar 06 '13 at 17:12
  • @Colin I created a replace regular expression, need to replace more than just tbl, in all the places that the templates generated the class names based on the table names. e.g. in Context.tt `var regEx = new Regex("(?i)^(tblAA)|(tblBB)|(tblCC)");` ... `public DbSet<<#= regEx.Replace(set.ElementType.Name, string.Empty)#>> <#= regEx.Replace(set.Name, string.Empty)#> { get; set; }` – Patrick Mar 08 '13 at 13:26
  • I just had to replace "tbl" when it occurred as a prefix so I was just using Name.StartsWith("tbl") ? Name.Substring(3) : Name; I actually thought the hard bit was locating "all the places that the templates generated the class names based on the table names". I've switched to using the designer instead. You have to change each name manually by double-clicking on it and editing it, but you only have to do it the once. – Colin Mar 08 '13 at 14:28
  • @Colin - I just checked and between the 3 files (Context, Entity and Mapping) I had to add my "Hack" 21 times... I hope there is just something I'm missing. I'll need to do more research. If I find anything I'll report back here. – Patrick Mar 08 '13 at 17:58

1 Answers1

3

Patrick, what you need is to modify the T4 template used by the EF Power Tools. When you want to create a code-first with all the mappings, instead of Reverse Engineer Code First option, choose Customize Reverse Engineer Template. You should get three files:

  1. Context.tt
  2. Entity.tt
  3. Mapping.tt

For example, in Mapping.tt there is a line that reads MetadataProperties from a TableSet, and which extracts Table name. The line looks like this:

var tableSet = efHost.TableSet;
var tableName = (string)tableSet.MetadataProperties["Table"].Value ?? tableSet.Name;

This is where you need to make changes and do something like:

var newTableName = tableName.Replace("tbl", String.Empty);

Of course, you should opt for a different strategy and use Substring method or Regular expression to read and remove the first three characters. After that you have to go through the tt file and use your logic where you want to use tableName and where to user newTableName variable. You will keep tableName where the mapping is done with the table in a database, and newTableName where you want to use that name for your POCO classes and filenames.

Repeat the process for the other two files. For more information have a look at Rowan Miller's blog article. This should give you a pretty good idea how to proceed.

Huske
  • 9,186
  • 2
  • 36
  • 53
  • I think this is what we both did before (well Patrick did. The table name is extracted in multiple places in the files and I gave up trying to track them all down) - and it doesn't change the output file name. – Colin Mar 13 '13 at 13:50
  • @Husein I was able to make all the changes but it does not change the file name that is generated. – Patrick Mar 13 '13 at 19:41
  • Actually this is true. My answer works on class names and mapping classes, but the output filename seems to be controlled by `CodeGenerationTools` class (you can see it at the top of each of the three .tt files). This class is defined in a file EF.Utility.CS.ttinclude which is located in `VS2012 Install Folder\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes`. It is actually using type's Name property to generate file names. – Huske Mar 13 '13 at 21:00
  • @Husein - I'll give this a try and see if it corrects the file names. Thanks for your feedback! – Patrick Mar 15 '13 at 15:01