Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Entity Framework generated classes to have Pascal casing and column names to have Camel casing

I have been working with Entity Framework 4 and SQL Server. The main problem I have found is that the table names in the database are all lower case and has underscore. This means that when I create the entities in Visual Studio, the classes and the properties are all lower case with underscores Is there any way to achieve Pascal Casing for the classes created and Camel Casing for the Properties?

Eg:

table_name--> to be converted as TableName

Is there any other templates need to be added or any other way to achieve this.

Editing the class name and properties manually in is not recommended as i have huge number of entities

like image 962
Xavier Avatar asked Feb 28 '13 09:02

Xavier


1 Answers

Why not use a T4 template to generate the entity classes? That way, you can add a method to convert the table names to the convention of your choice.

VS comes with a couple of built-in T4 templates for EF, so it's very likely that you can just pick one of these and modify it. I wouldn't recommend writing your own from scratch!

If you haven't used T4 templates before, a quick start is to open your EF model in the designer, right-click a blank part of the design surface and choose "Add Code Generation Item." This will open a dialog with the installed T4 templates for EF, and you can choose whichever you feel most appropriate.

You can then right-click the T4 template files in Solution Explorer (it will have a .tt extension) and choose "Run Custom Tool" to generate the entities themselves. You can edit the .tt file (it's just a plain text file, containing something that looks horribly like VBScript!) and make the modifications. Then run the custom tool again and see if the generated entities have the right names.

Hope this helps.

like image 199
Avrohom Yisroel Avatar answered Nov 07 '22 01:11

Avrohom Yisroel