Search code examples
entity-frameworkef-power-tools

Change name of generated Context file with EF PowerTools Reverse Engineer Code First


I have been attempting to figure out how to make the EF Power Tools - Reverse Engineer Code First use a different name for the generated Context-file, than what it uses now.

Example

I have a database called My_Awesome_Dev_Database. When I run Reverse-engineer against that, the file that is generated will be called:

My_Awesome_Dev_DatabaseContext.cs

What it would like to do is specify what the file is to be called, for instance:

MyAwesomeDatabaseContext.cs

Attempts so far

I have tried looking through the EF.Utilities.CS.ttinclude file, to figure out how the filename is generated - but I have been unsuccessful so far.

Does anyone know ?

Thanks in advance!


Solution

  • Currently the generated context file naming convention is hard-coded and non configurable.

    All the logic is inside the ReverseEngineerCodeFirstHandler class (the source is on CodePlex).

    It sets the context file name and path with

    var contextFilePath = Path.Combine(modelsDirectory, 
         modelGenerator.EntityContainer.Name + contextHost.FileExtension);
    var contextItem = project.AddNewFile(contextFilePath, contextContents);
    

    So the file name is coming from modelGenerator.EntityContainer.Name which gets created upper in the method with:

    var contextName = 
        connection.Database.Replace(" ", string.Empty)
                           .Replace(".", string.Empty) + "Context";
    var modelGenerator = 
        new EntityModelSchemaGenerator(storeGenerator.EntityContainer, 
            "DefaultNamespace", contextName);
    

    So as you can see the tool just takes the db name removes the spaces and dots and use it as the context name which will end up as the generated file name.

    You can open an issue or - because Entity Framework is open source - take the code, add this configuration option, and send back a pull request.