57

I've been playing around with Entity Framework 7 and ASP.NET 5 for a new project I'm working on, but I've hit a roadblock. The team I'm working on uses a DBA-first approach to development; i.e. the database is designed by DBA's and then the developers alter the code to compensate for the model changes.

Using EF6, this works well, since we can just update the code using the EDMX designer's "update" functionality. One click, we get the new classes, and we're done. However, in EF7, everything is different. There's no more designer, and we're supposed to use Code-First, which according to some blog postings out there by the EF team, should also support "Database-First" code generation.

However, I'm unable to figure out how to do this with the Visual Studio 2015 CTP6 in an ASP.NET 5 application. Is the tooling support there yet, or am I out of luck? And is it even coming at all?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Ron Penton
  • 1,511
  • 1
  • 12
  • 24
  • look up "reverse engineer code first" – DLeh Mar 27 '15 at 12:34
  • 1
    Yeah I've spent a few days searching. Lots of information on how to do this with EF6, but with EF7 there doesn't appear to be a way. For the time being, I've created an EF6 project, done the reverse-engineering there, and copied the files over to my EF7 project (with some modifications due to non-existent namespaces in EF7), but in the long term I'm going to need a more stable solution. – Ron Penton Mar 27 '15 at 12:55
  • 1
    i don't think they're different enough to make that much of a difference. I think EF7 is still in beta, so there probably isn't full support for it yet. https://github.com/aspnet/EntityFramework/releases – DLeh Mar 27 '15 at 12:56
  • As a warning to any readers, the process is still full of bugs. These answers may help you, but there are still bugs at this stage, and some people just won't be able to get it to work. – Palu Macil Mar 01 '16 at 19:18
  • Update for VS2017: if you are using the latest and greatest microsoft product suite, here is their tutorial: https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db – smurtagh Nov 01 '17 at 18:34

4 Answers4

56

In the latest bits it is possible to use the dnx command prompt and PowerShell commands to do this, yes

Scaffold-DbContext '<connectionString>' EntityFramework.MicrosoftSqlServer

or

dnx ef dbcontext scaffold "<connectionString>"  EntityFramework.MicrosoftSqlServer

or (from EF Core RC2)

dotnet ef dbcontext scaffold "<connectionString>"  Microsoft.EntityFrameworkCore.SqlServer

You must install the Microsoft.EntityFrameworkCore.Tools package for the command to work.

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • Can you please tell me where to run this command.. where to find dnx command prompt – Pooran Jul 23 '15 at 04:16
  • 1
    my bad .. got it http://blogs.msdn.com/b/sujitdmello/archive/2015/04/23/step-by-step-installation-instructions-for-getting-dnx-on-your-laptop.aspx – Pooran Jul 23 '15 at 04:22
12

This can be done using NuGet package manager console or command prompt. I tried with command prompt. After navigating to the project folder in command prompt, I used a similar command:

dnx ef dbcontext scaffold "Data Source=myServerName; Initial Catalog=myDatabaseName; Integrated Security=True" EntityFramework.SqlServer

I got errors about the missing packages:

EntityFramework.Commands

EntityFramework.SqlServer.Design

followed by this command before proceeding:

dnu restore

The actual package(s) required may vary based on the mentioned framework(s) in you project.json file.

If you are unable to execute the dnx or other related commands at the command prompt, follow THIS link which has been mentioned in comments of another answer.

P.S. : Here is the current command list [at the time of writing, last updated Aug 21]:

ASP.NET - EntityFramework Wiki -- NuGet/DNX Commands

Community
  • 1
  • 1
BiLaL
  • 708
  • 11
  • 18
9

Here are the updated parameters for RC2 of .NET Core (May 2016)

dotnet ef dbcontext scaffold -c RRStoreContext -o Model 
"Data Source=(local);Initial Catalog=DBNAME;Integrated Security=True"     
Microsoft.EntityFrameworkCore.SqlServer --force

Note that Microsoft.EntityFrameworkCore.SqlServer is the new name for the package that needs to be used in the command. I've added the force parameter to overwrite existing files. The 'o' parameter is output directory name. And it's now dotnet instead of dnx.

As of the current release the dependencies you need in your project.json are

"dependencies": {
    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-rc2-final",
    "Microsoft.EntityFrameworkCore.Tools": {
      "type": "build",
      "version": "1.0.0-preview1-final"
    }
  },

Note: Type of 'build' means that anything referencing your assembly won't take this DLL as a dependency since it's only needed for tooling.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
2

@ErikEJ has developed a very useful tool for this. Which generates POCO classes just by doing right click on the project and provide you SQL server instance.You can download it from here and steps are clearly mentioned here.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197