“Build failed” on Database First Scaffold-DbContext

谁说我不能喝 提交于 2019-11-30 04:10:48

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

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

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>

Using VS2017 Preview 3, .NET Core 2 (PREVIEW) I had all sorts of issues, but eventually I took the approach suggested above and created a brand new solution.

  1. Created new .NET Core solution
  2. Edited project file and changed 1.0 to 2.0: <TargetFramework>netcoreapp2.0</TargetFramework>
  3. Closed/re-opened solution

Then, added the Entity Framework:

  1. In PackageManager console:
    • Install-package Microsoft.EntityFrameworkCore.SqlServer -Version 2.0.0-preview2-final
    • Install-package Microsoft.EntityFrameworkCore.Tools -Version 2.0.0-preview2-final
    • Install-package Microsoft.EntityFrameworkCore.Design -Version 2.0.0-preview2-final
  2. Edited project file, and added: <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.Dotnet" Version="2.0.0-preview2-final" />

Then;

  1. Opened the Powershell command prompt and changed directory into Scaffold project folder
  2. Ran: dotnet ef dbcontext scaffold "Server=DESKTOP-MB70B7U; Database=ForexForme; Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -o Models
    • Where you put in your own connection string!
    • Models is the name of my directory where I put all my classes

For me the issue was that I was trying to set it up in a new blank console project inside of the solution, which had no files, so the scaffolding tried to use this project as a startup project and couldn't find Main. I fixed it by adding a new file with an empty main

Build complete solution and see where it fails. I had some NuGet projects hidden away in a folder that didn't build. Only while rebuilding the solution I found out what the problem was. Everything needs to build or else Scaffold will fail.

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.

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

If you use multiple projects in the solution, check the default project in the package manager.

If entity-framework returns build failed, most probably you have some kind of error in any of your projects.

Even if the project you are running the command on, is clean and error-free, other projects in that solution can cause the build failed response.

Solution

  • Rebuild the whole solution. Most probably you'll find that error in solution-rebuild process.
  • Make sure the project you want to run command on, is selected in Default project drop-down inside Package Manager Console
  • Re-run the command.

For me, my project built in Visual Studio but I had to specify a version for "Microsoft.AspNetCore.App" when running Scaffold-DbContext.

So instead of:

<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.1.6</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>

I had to have:

<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.6" />
</ItemGroup>

Thanks to the above, rebuilding the project solution solved this. Some crucial caveats for me personally were:

  1. Running the dotnet build was not enough (I had asssumed it was)!
  2. In visual studio menu, Build > Build solution (Ctrl + Shift + B)
    • I believe I was simply trying to run the dotnet build command while inside a child project (myProject.data)
    • Rebuilding the parent project (myProject) solution was the key

I hope that helps someone else who was equally confused!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!