Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Build failed" on Database First Scaffold-DbContext

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.

like image 517
LightToTheEnd Avatar asked Aug 15 '16 18:08

LightToTheEnd


5 Answers

Two most important tips:

[1] - Make sure that your project builds completely before you run a new scaffold command.

Otherwise...

  • You'll start writing a line of code.
  • You'll realize a required DB column is missing from your model.
  • You'll go to try to scaffold it.
  • Twenty minutes later you'll realize the reason your build (and scaffold command) is failing is because you literally have a half written line of code. Oops!

[2] - Check into source control or make a copy:

  • Allows you to easily verify what changed.
  • Allows rollback if needed.

You can get some very annoying 'chicken and egg' problems if you get unlucky or make a mistake.


Other problems:

If you have multiple DLLs make sure you aren't generating into the wrong project. A 'Build failed' message can occur for many reasons, but the dumbest would be if you don't have EFCore installed in the project you're scaffolding into.

In the package manager console there is a Default project dropdown and that's probably where your new files ended up if you're missing an expected change.

A better solution than remembering to set a dropdown is to add the -Project switch to your scaffolding command.

This is the full command I use:

For EF Core 2

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

For EF Core 3

dotnet ef dbcontext scaffold "Server=tcp:XXXXX.database.windows.net,1433;Initial Catalog=DATABASE_NAME;Persist Security Info=False;User ID=USERNAME;Password=PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -o DB.Models --context-dir DB.Contexts --context RRDBContext --project RR.EF.csproj --force --use-database-names

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 Scaffolding reference:

EF Core 2:

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

EF Core 3:

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

like image 140
Simon_Weaver Avatar answered Nov 05 '22 08:11

Simon_Weaver


Manually building the project by pressing Ctrl+Shift+B helped me to see the errors that were causing the build to fail.

like image 22
Mxaza Avatar answered Nov 05 '22 07:11

Mxaza


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>
like image 13
melicent Avatar answered Nov 05 '22 07:11

melicent


I still had this problem even when I ensured that my project (which had EF Core installed) built correctly. It still failed with the "Build failed." message, which is visible when using the -Verbsose flag.

I had to do this in my case:

  • Create a throw-away ASP.NET Core web application solution
  • Add the EF Core NuGet package to the solution
  • Add the EF Core Sql Server provider NuGet package (because I'm using SqlServer)
  • Add the EF Core Tools NuGet package
  • Switch -Project in the package manager console command to point to my newly-created (and EF Core-provisioned) project. The last step was just for good measure, since there was only one project in my throw-away solution.

It seems that this whole process requires an ASP.NET core project (or just a .NET Core project that isn't a class library) somewhere in the solution, presumably set as the solution startup project too.

like image 6
bcr Avatar answered Nov 05 '22 08:11

bcr


Make sure your project isn't running, for some reason this command doesn't work while my API is running in the background.

like image 5
Josh Siegl Avatar answered Nov 05 '22 07:11

Josh Siegl