I have an existing database with tables that have a prefix
ex) - px_mytable1 - tx_mytable1
When I use entity framework to generate models from my database, the prefix is included in the model name..
ex) - public partial class px_mytable1 - public partial class tx_mytable1
How do I configure entity framework to remove the prefix from the model name?
Note: I am using an .edmx file to generate models from the database schema.
You have to do some changes to the t4 template files(.tt). first o all in yourModelName.tt file add the following variable in in of the 5,6,7 ... 13 lines.
var tablePerfix="px_";
FInd the following code
`<#=codeStringGenerator.NavigationProperty(navigationProperty)#>`
and replace it with
<#=codeStringGenerator.NavigationProperty(navigationProperty).Replace(tablePerfix,"")#>
Find the following code
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>
(itemCollection))
{
fileManager.StartNewFile(entity.Name + ".cs");
BeginNamespace(code);
#>
replace it with
foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>
(itemCollection))
{
fileManager.StartNewFile(entity.Name.Replace(tablePerfix,"") + ".cs");
BeginNamespace(code);
Apply .Replacy(tablePerfix,"")
like following to remove prefix from entities.
<#=codeStringGenerator.EntityClassOpening(entity).Replace(tablePerfix,"")#>
// ...
public <#=code.Escape(entity).Replace(tablePerfix,"")#>()
// ...
this.<#=code.Escape(navigationProperty).Replace(tablePerfix,"")#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType()).Replace(tablePerfix,"")#>>();
if you have 2 prefixes for the tables in the DB you should use Replace method twice for each prefix
Finally, in yourModelName.Context.tt file add a variable called tablePrefix
, and change following code
<#=codeStringGenerator.DbSet(entitySet)#>
To
<#=codeStringGenerator.DbSet(entitySet).Replace(tablePerfix,"")#>