"Build failed" on Database First Scaffold-DbContext

c# entity-framework-core


I'm trying to generate classes from a database (EntityFramework's database first approach).

For convenience, I'm more or less walking along with this tutorial: https://docs.efproject.net/en/latest/platforms/full-dotnet/existing-db.html

I'm at a point where I am running the equivalent of this line of code in the Visual Studio Package Manager Console:

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Verbose

This line of code is generating the error (with -Verbose mode on):

Using startup project 'EFSandbox'.
Using project 'EntityFrameworkCore'
Build started...
Build failed.

I see no other options that produce any meaningful output, and I see no documentation on this particular error. If it helps at all, this project does not have a project.json file, currently. Everything is in the .csproj file, which I have not manually edited.

8/15/2016 6:49:34 PM

Accepted Answer

First make sure that your project builds before you run a new scaffold command.

What often happens to me is I'll start writing a line of code, realize a new DB column is needed, go to try to scaffold it - then realize after 20 minutes the reason my build (and scaffold command) is failing is because I have a half written line of code. Oops.

If you have multiple DLLs you may be generating into the wrong project. Then 'Build failed' is occurring for any number of reasons, quite possibly that you don't have EFCore installed into that project.

In the package manager console there is a Default project and that's probably where your files ended up.

The solution is simple and you just need to add the -Project switch to your options.

This is the full command I use:

Scaffold-DbContext -Connection "Server=(local);Database=DefenderRRCart;Integrated Security=True;Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir RRStoreContext.Models -context RRStoreContext -Project RR.DataAccess -force

Note: -force will overwrite files but not remove ones that don't exist any more. If you delete tables from your DB you must delete the old entity files yourself (just sort in Explorer by date and delete the old ones).

Full options: https://docs.efproject.net/en/latest/miscellaneous/cli/powershell.html#scaffold-dbcontext

9/20/2018 8:35:19 PM

Popular Answer

I know this is old, but I spent a while trying to figure this out today, so I hope this helps someone.

I have a .Net Core project but I want to scaffold my files into a .Net Standard class library. DbContext-Scaffold in the package manager console didn't work for me, but dotnet ef dbcontext scaffold in a regular command prompt did.

I had to install these packages in my class library:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools

I had to have a .Net Core project set as the startup project in my solution and that project had to have a reference to my class library. I think that last part is what I was missing that kept me scratching my head for so long.

Finally, I cd'd into the class library from a command prompt and ran this:

dotnet ef dbcontext scaffold "<connection string>" Microsoft.EntityFrameworkCore.SqlServer -o <output folder> -s <relative path to my startup project>

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow