The database I’m working against has table names such as “table_name”. That’s fine, but I’d like to generate classes in the format “TableName” to work with in C#, Pascal style.
Is this possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Update: For use with EF6, see additional answer on this page.
Thanks to Alex’s answer, I’ve now extended this code to a full working solution that solves this problem. Since it’s taken me most of the day, I’m posting here to help others facing the same challenge. This includes a complete class for manipulating the edmx file (no points for pretty code here) which passes fairly verbose unit tests on varying input strings:
A few examples:
some_class > SomeClass
_some_class_ > SomeClass
some_1_said > Some1Said
I had some additional issues to deal with:
Firstly optionally not replacing underscores while still changing the string to pascal case (due to column names like “test_string” and “teststring”, which both resolved to “TestString” otherwise, causing collisions).
Secondly, my code here takes an input parameter that modifies the object context class name that is generated.
Lastly, I wasn’t initially sure how to get the designer file to update after modifiying the edmx, so I’ve included exact steps. I would recommend creating a pre build step in Visual Studio, which saves some of the remaining effort in updating from a modified database schema.
Updating an EDMX File to Reflect Database Changes
Double click the edmx file to show the design surface.
Right click the design surface and select “update model from database”.
Select “yes include sensitive information in connection string”.
Uncheck “save entity connection settings in App Config” (we already have these).
Select the appropriate database. In Add screen select Tables, Views etc.
Leave pluralise and foreign key options as checked. If creating the edmx file from fresh, you
are given the option to enter a model name. You can change or leave this.
Click finish.
Run the code below on the edmx file.
Depending on the state of the application, you may see errors here until the next step.
Right click the edmx file that you are updating, and select “Run custom tool” in Visual Studio. This will cuse
the designer.cs (C#) file to be updated.
Run build to check there are no compiler errors.
Run tests to ensure application is functioning correctly.
Any application issues following this should be expected according to the application
changes that have been made.
Replacing an edmx file in it’s entirety.
Delete the edmx file, taking the designer file with it.
Right click the entities folder
From the create file dialogue, select ADO.NET Entity Data Model. Name it accroding to the class name you would like for your object context(IMPORTANT).
This value is referenced in the connection string, so take a look at your app config in case of issues.
In choose model contents, select generate from database.
Follow above instructions from step 3.
Edit by Chris
Adapting to Visual Studio 2013 & EF6
The code namespaces need a little tweak in order to make it work with EF6:
plus you need to take care of designerDiagram(in my case, it wasn’t found, so just replaced First() with FirstOrDefault() and added simple null check).