Reputation: 5554
Guys am working on an Asp.Net MVC project that already is using a repository pattern for fetching data from MS SQL Server database.
I now intend to use Entity Framework for some new particular requirement of our project without disturbing the old repository logic. I have used EF Power tool Beta 3 and Reverse Engineered my database tables from Visual Studio, but the generated classes conflict with some of my existing repository classes which have the same name to their corresponding table in database, making the project unable to compile. I have looked upon this tutorial
But this tells about appending "tbl" prefix to table names and not changing generated class names. I myself however tried to change the generated class names in templates (present under CodeTemplates\ReverseEngineerCodeFirst but still they are generating with conflicts.
My question is what should I do to achieve my intended result (no conflicts with existing classes in project) or should I adopt some other alternative altogether ? Thanks for helping.
Upvotes: 1
Views: 3399
Reputation: 2417
I know it is an old question but this can come handy to somebody.
I needed to rename scaffolded classes from DBS as well. In my case I wanted to add Entity
suffix.
So in case you use EF Power Tools create on the TOP of project structure file:
efpt.renaming.json
with following content:
[
{
"SchemaName": "App", // <----- change this
"TableRegexPattern": "$",
"TablePatternReplaceWith": "Entity",
"UseSchemaName": false
},
{
"SchemaName": "Enum", // <----- change this
"TableRegexPattern": "$",
"TablePatternReplaceWith": "Entity",
"UseSchemaName": false
},
{
"SchemaName": "LinkTable", // <----- change this
"TableRegexPattern": "$",
"TablePatternReplaceWith": "Entity",
"UseSchemaName": false
}
]
Each DBS schema should be specified, in my case 3.
Then make sure in your file efpt.config.json
you have specified value:
"UseDatabaseNames": false
because if it is true
those renaming changes wont be applied.
You can also find this option in the EFPT User Interface here (make it disabled):
After all these changes are done then start EF Power tool reverse engineering and your new entities should be created with new names.
Upvotes: 0
Reputation: 5554
OK taking into consideration @jlew's suggestion, I found a workaround and yes it clicked:
Here it is:
First I did an overwrite of default Templates generated under "CodeTemplates/ReverseEngineerCodeFirst" folder by the customized templates downloaded from the following link:
http://romiller.com/2012/05/09/customizing-reverse-engineer-code-first-in-the-ef-power-tools/
(Note: This is done in order to use Data Annotations for configuration, rather than the Fluent API and so that column and table mappings are done with Data Annotations. For details see the above link).
Then I did following:
In Context.tt file replaced
using <#= code.EscapeNamespace(efHost.MappingNamespace) #>;
namespace <#= code.EscapeNamespace(efHost.Namespace) #>
with
<#
var hostMapNamespace = code.EscapeNamespace(efHost.MappingNamespace);
hostMapNamespace = hostMapNamespace.Replace(".Models", ".ModelsEF");
var hostNamespace = code.EscapeNamespace(efHost.Namespace);
hostNamespace = hostNamespace.Replace(".Models", ".ModelsEF");
#>
using <#= hostMapNamespace #>;
namespace <#= hostNamespace #>
2- In Entity.tt file replaced
namespace <#= code.EscapeNamespace(efHost.Namespace) #>
with
<#
var hostNamespace = code.EscapeNamespace(efHost.Namespace);
hostNamespace = hostNamespace.Replace(".Models", ".ModelsEF");
#>
namespace <#= hostNamespace #>
3- In Mapping.tt file replaced
namespace <#= code.EscapeNamespace(efHost.Namespace) #>
with
<#
var hostMapNamespace = code.EscapeNamespace(efHost.Namespace);
hostMapNamespace = hostMapNamespace.Replace(".Models", ".ModelsEF");
#>
namespace <#= hostMapNamespace #>
Well in short, I have somewhat changed namespace generation in custom templates so as to avoid any conflicts with any existing classes of project especially in the Models namespace. Good thing is... it works!
Upvotes: 0
Reputation: 10601
I don't know about getting the tool to do what you ask, but how about changing the namespace in your generated file, so the duplicate names won't matter?
Upvotes: 1