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

余生颓废 提交于 2019-12-10 15:56:42

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/19181338/change-name-of-generated-context-file-with-ef-powertools-reverse-engineer-code-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!