Is there an Entity Framework 7 Database-First POCO Generator?
Asked Answered
H

4

58

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?

Hickie answered 27/3, 2015 at 12:33 Comment(5)
look up "reverse engineer code first"Aloin
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.Hickie
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. github.com/aspnet/EntityFramework/releasesAloin
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.Owing
Update for VS2017: if you are using the latest and greatest microsoft product suite, here is their tutorial: learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/…Valorie
G
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.

Gaur answered 27/3, 2015 at 13:4 Comment(2)
Can you please tell me where to run this command.. where to find dnx command promptTempleton
my bad .. got it blogs.msdn.com/b/sujitdmello/archive/2015/04/23/…Templeton
T
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

Terricolous answered 28/10, 2015 at 16:52 Comment(1)
added a separate answer for the new syntax for .NET Core RC2 (May 2016)Butyrate
B
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.

Butyrate answered 25/5, 2016 at 8:7 Comment(3)
Also, it's worth noting that the --force flag overwrites existing filesRhizomorphous
And -o is the name of the folder you're outputting to. That is generally ModelsRhizomorphous
Here is a link to the documentation for this.Constringent
R
3

@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.

Rascal answered 30/3, 2018 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.