Search code examples
entity-frameworkvisual-studio-2012ef-code-firstreverse-engineering

Reverse engineering a subset of tables for Entity Framework


I'm using EF, and I've got the Entity Framework Power Tools extension, which lets me reverse engineer classes based on tables in a given database.

We have a rather extensive DB, with a lot of tables that I do not need to represent. Is there any simple way to select a subset of these, and reverse engineer only those?


Solution

  • I found a solution which works, though it requires a couple of "extra" steps:

    In the project, right click and select Add > new Item. Select Data on the left, and ADO.NET Entity Data Model from the items list.

    From the dialog / wizard that shows up now, you can select a DB to generate a model from, and in this case, you can select only the tables, views and stored Procedures and functions you need.

    After adding the model, you can expand it in the solution explorer, and you should find the classes you need under YourModelName.edmx --> YourModelName.tt

    Note: You won't be able to drag and drop the source-files out, but you can right click and choose copy class. You can then paste them where you need them, and they will appear with a "1" appended to their names. You will therefore need to do some light refactoring (changing the name of the files, the classes, and the namespaces), but it is still fairly straight forward.

    Now you can delete the original model (the edmx-file), and everything under it.

    This is not a perfect solution, but it`s still easier than having to generate a huge number of table-representations and wait for VS to complete the job, especially if you've got a lot of unnecessary tables, views, etc.