Adventures with MVC 6 - Episode III : Scaffolding the EF7 Class Library

Many of the projects I'm tasked with use an existing SQL Server database. I've developed a pretty standard set of steps now when I need to set up the Entity Framework 7 (EF7) libraries the rest of my solution will use. See the previous post about setting up the solution for more context.

After I've added my Class Library (Package) project, Acme.Disgronifier.EF, I add in the necessary dependencies and commands to the project.json file. All I'm doing is typing in the project.json file, there is no magic to this step. It should look like this in the end. My changes from the originally generated file are highlighted.

Code

I removed any frameworks except the dnx451 framework. We will want this one over the net451 framework so that we can run dnx related commands against this project from the command line. We removed the dotnet5.4 framework reference entirely because, like I said before, it won't gain us anything in our solution yet. It's more of a hassle since so many of our dependencies just don't support it yet.

I added the following project dependencies which are necessary to help scaffold out entities from an existing SQL Server database:

    "EntityFramework.Commands": "7.0.0-rc1-final",
    "EntityFramework.Core": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
    "EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final"

Finally I add in a command that we can reference from the command line on this project:

   "ef": "EntityFramework.Commands"

Next I open a command prompt window and navigate to the same folder as the project.json file. I run the following from the command line:

   dnx ef dbcontext scaffold "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=True" EntityFramework.MicrosoftSqlServer --table "dbo.Table1" --table "dbo.Table2" --context "DisgronifierContext" --dataAnnotations --outputDir "Models"

Be sure to replace the quoted string following "scaffold" with whatever connection string you need. You can scaffold as many tables as you like in one shot. I recommend including all related tables so that EF7 can model the appropriate relationships.