Real-world Entity Framework :: Generating Entities from Existing Database
Code First workflow is cool; but in real world; you don’t start from scratch every day! For such new cool things; often; you have to create space in existing projects and solutions. With Entity Framework 6; you can mix Code First’ DbContext and Model First’ ObjectContext in single assembly, you might try creating new entities and the required DbContext and mappings; and then left wondering; ok this is cool; but how can I bring my existing database tables into the project and don’t have to write all entities and their required mappings from scratch. As Hanselman says; this is monkey code and shouldn’t be written; this is where Entity Framework Power Tools comes in. These tools are made by the EF team; so this is high quality stuff. Similar to Model First workflow; these tools allow us to generate entities and mappings from existing database; these files get generated into the project \ Models folder. We can customize the generated code by editing the T4 templates; however this tool generates entities for all tables in single run; unlike Model First; no option to select tables or views! This is time consuming for large databases; but its ok; as generally its one time thing we selectively “include” the generated entity and their mapping; or better copy the code from generated files over to their “proper” places. Don’t forget to include the related mappings and go through the generated code!
The EF Power Tools comes with other such right click features; explore them right clicking DbContext and *.edmx files or read about them at the power tools page on Visual Studio Gallery!