EF Core add-migration Build Failed
Asked Answered
H

39

119

I have a developer that is getting "Build failed." when running add-migration in a .NET Core EF project, with no explanation of why the build failed. How do you troubleshoot this error?

This is what he gets in the Package Manager Console:enter image description here

Additional information:

We have a few other developers using the same solution code (myself included) that have not issues with add-migration.

This is what I see in Package Manager Console:enter image description here

We've verified that the project builds, and the entire solution builds. We've done "dotnet restore" and rebuild all multiple times, in addition to restarting VS2015. We've verified that the correct default solution is selected both in Solution Explorer, and in the Package Manager Console drop-down. We've verified that he has the correct SDK installed on his machine. I'm at a loss as to what to check next...any time I've had a failure during add-migration I've gotten enough information to point me in the direction of what to check, but just "Build failed." is a fairly useless error output.

Hasen answered 27/6, 2017 at 16:33 Comment(8)
You should try to use the -v option and see if you get more details about the error.Allies
When I get build errors on just a single machine, I start be deleting personal solution file (.suo). You will need to reset the startup project after that.Hydroxyl
We used the -v option, we did not get more details about the error.Hasen
Deleting the .suo file didn't change anything.Hasen
execute "dotnet build" command and see more details about the error.Tevis
Solved this problem by rebuild the project 'dotnet run'.Con
The -v option helped! A note to future travelers, you may want to redirect the output to a text file, the problem is probably near the top of the output. And -v, the verbose flag, means what it says. It is very, very verbose.Potion
"dotnet build" worked for me.Bennettbenni
H
1

The developer ended up un-mapping the project from TFS, deleting it, and re-mapping it. It's now working for him.

Hasen answered 27/6, 2017 at 21:38 Comment(1)
This would be my last resort.Incorruption
S
127

It's because of other projects in your solutions. e.g If you have two projects in your solution as Project A and Project B. If Project A is not successful build and then Project B data migration will fail. So you should unload Project A and add the migration to Project B again.

I think this is not a good answer but hope this help.

Subaquatic answered 4/9, 2017 at 16:6 Comment(3)
I had an unwanted database project in my solution which was added as a random test and I was not aware of it! When i built the solution, no error shown but the add-migration Build Failed! After i remove that temp project, everything works!Eldenelder
I had the dotnet run watch running and was holding everything upProcopius
The easiest way to identify the problem: 1. open command-line 2. navigate to project folder where you want to add the migration 3. run dotnet build The output will show all errors and warningsEmperor
E
55

I had the exact same problem (.NET Core 2.0.1).

Sometimes it helps if the project is rebuilt.

I also encounter the problem when I opened the project in 2 Visual Studios.
Closing one Visual Studio fixed the error.

  • Ctrl + C (first stop the application from running, this alone might be enough!)
  • dotnet run build
  • dotnet ef migrations add InitialCreate
Eustache answered 12/12, 2017 at 15:19 Comment(3)
Rebuilding the project after changing the connection string on the appsettings.json works for me.Kelleykelli
I had the solution open in VS2019 and simply closing VS2019 made the command work.Ogren
I closed all open visual studios, it fixed.Shumway
S
29

I was struggling with this one while using Visual Studio 2019 version 16.5.0.

What solved it finally:

  • Close Visual Studio;
  • Reopen Visual Studio;
  • Make sure you right click the Solution if you have multiple projects and select Rebuild Solution.

Note that I tried doing a Rebuild Solution before and that did not catch the error. So it's important to close VS and reopen it.

It then showed the error that was causing add-migration command to fail with Build failed message.

Skiing answered 17/3, 2020 at 17:29 Comment(2)
thanks! simple ... it was just a matter of closing and reopening visual studio :)Ithaman
That you very much!Unhopedfor
S
24

I suggest adding -v to have more info.

In my example, I had to add <LangVersion>latest</LangVersion> to my projects, as this was missing from some of them.

Stavro answered 12/4, 2019 at 8:49 Comment(0)
T
18

Try these steps:

  1. Clean the solution.

  2. Build every project separately.

  3. Resolve any errors if found (sometimes, VS is not showing errors until you build it separately).

  4. Then try to run migration again.

Tusk answered 26/11, 2018 at 13:23 Comment(0)
C
15

In my situation it was the error

namespace aspnetcore does not exist .. are you missing a reference?

I have figured it out by adding the -v (or --verbose) flag to the dotnet ef command.

Cacodemon answered 21/4, 2018 at 21:9 Comment(1)
adding -v saved me.Stavro
W
12

In my case, the application was running. Apparently, it needs to be stopped. Weird error message anyways.

Waft answered 9/5, 2019 at 18:8 Comment(2)
Same. IISExpress was hosting my web. Closed VS2019 (which closes IISExpress) and the migration succeeded.Ogren
Same here. I closed VS2022 and Sign out my PC (which closes every running program) then the migration succeededHrvatska
T
10

Had the same situation. This helped:

  1. Open Package Manager Console

  2. Change directory to Migrations folder:

    cd C:\YourSolution\YourProjectWithMigrations
    
  3. Enter in PM:

    dotnet ef migrations add YourMigrationName
    
Tatary answered 2/2, 2018 at 9:15 Comment(0)
T
10

Open Output window in Visual Studio and check your build log. In my case, even though my current configuration was Release, Add-Migration built the project in Debug, which had an error. For some reason, VS didn't display the error anywhere except Output window for me.

Ternan answered 28/6, 2018 at 8:28 Comment(2)
Check in Output windows is professional way. We can know exact root cause why build failed.Ulrike
So stupid of me not to think of it but there was no hint to look at that window (it was hidden by the PM Console.. But many thanks for mentioning it!Muricate
A
7

Got the same error when I tried to run add-migration. Make sure that you don't have any syntax errors in your code.

I had a syntax error in my code, and after I fixed it, I was able to run add-migration.

Allowance answered 8/12, 2019 at 17:29 Comment(1)
I made some hefty changes to models and (re)building would not show any errors, but add-migration just said "Build failed". Running the command line yielded the actual errors: in this case it was the Razor Views! Fixed those and add-migration worked again.Cornucopia
V
3

Most likely that there is an existing error in your code. Try rebuilding first and correct all the errors.

Happened to me, I am trying to rollback migration but I have not corrected the errors in the code yet.

Vuong answered 30/7, 2018 at 10:53 Comment(0)
G
3

Just got into this issue. In my case, I was debugging my code and it was making my migrations fail.

Since in Visual Studio Code, the debugger is so low-profile (only a small bar), I didn't realize about this until I read some questions and decided to look better.

Groundling answered 25/4, 2019 at 14:58 Comment(0)
R
2

I had exact same error but I am using Visual Studio Community 2017 Version 15.2 (26430.14) to build .Net Core projects.

I have a ASP.NET Core MVC web project and a separate security project using ASP.NET Core Identity. The web project contains connection string in aspsettings.json config file.

I also installed Bundler & Minifier and Web Essentials 2017 extensions in Visual Studio so that I can compile, minify and bundle my assets and put them to wwwroot.

I figured out it was the MSBuild those 2 extensions secretly download that caused the problem, because I had Enable Bundle on Build and Enable Compile on Build on. After I disable that, everything works fine.

Probably not the cause to your problem, but might be worthy to just give it a try.

Ridings answered 29/6, 2017 at 22:59 Comment(0)
G
2

I think this happened to me when I tried to run dotnet ef migrations add xyz whilst it was already running.

In the end, just to be sure, I stopped the currently running instance, then ran it again to ensure it was rebuilt, stopped it again and I was able to run the dotnet ef migrations add command

Guppy answered 19/4, 2018 at 15:40 Comment(0)
V
2

Try saving DataContext.cs first. It worked for me.

Vaniavanilla answered 4/10, 2018 at 20:12 Comment(0)
C
2

In My case, add the package Microsoft.EntityFrameworkCore.Tools fixed problem

Complicacy answered 20/8, 2020 at 15:3 Comment(0)
H
1

The developer ended up un-mapping the project from TFS, deleting it, and re-mapping it. It's now working for him.

Hasen answered 27/6, 2017 at 21:38 Comment(1)
This would be my last resort.Incorruption
F
1

I think the 2 commands below always work (if you run them at project's folder)

  • dotnet ef migrations add (database object fron ApplicationDbContextDBContext)
  • dotnet ef database update
Froth answered 29/6, 2018 at 10:24 Comment(0)
I
1

I had the same error. What I did, I corrected the error the one I have in code, and rebuilt the project. Then I ran the ms-dos command and it worked.

Inellineloquent answered 27/8, 2018 at 17:23 Comment(0)
S
1

I got the same error. There were no errors during the build proccess. I closed the Visual Studio and it worked.

Stephi answered 16/8, 2019 at 11:54 Comment(0)
G
1

I had the same problem when running: dotnet ef migrations add InitialCreate so what I did is tried to build the project using: dotnet build command.

It throws an error : Startup.cs(20,27): error CS0103: bla bla for example. which you can trace to find the error in your code.

Then i refactored the code and ran: dotnet build again to check any errors until there is no errors and build is succeded. Then ran: dotnet ef migrations add InitialCreate then the build succeded.

Gunplay answered 20/12, 2019 at 10:51 Comment(0)
U
1

this is because deleting projects or class libraries from solution and adding projects after deleting with same name. best thing is delete solution file and add projects to it.this works for me (I did this using vsCode)

Ursi answered 29/4, 2020 at 16:19 Comment(0)
S
1

In my case I had couple of such errors:

The process cannot access the file 'C:\xxx\bin\Debug\netcoreapp3.1\yyy.dll' 
because it is being used by another process. [C:\zzz.csproj]

I was able to see those errors only after adding -v parameter.

The solution was to shutdown the Visual Studio and run the command again.

Semitrailer answered 27/8, 2020 at 13:0 Comment(1)
Thanks for the "-v" switch. That helped! This poorly designed, manual EF solution by Microsoft means a million things can go wrong that block the "migration" junk files from getting built. In my case it didn't like some custom logging I was doing in my Main method. Who knew! Your -v or verbose allowed me to see all the errors and track it down. Otherwise this mess by Microsoft would have eaten up another half of my day :)Badillo
J
1

In my case rebuilding the Project worked fine. Running the project worked fine too and executing some endpoints worked.

But still on update-database it would say "Build Failed" without any clarification, even with -Verbose added to the command.

At the end it happened that there was an error in Unit Test project that would (for some reason) cause "Build Failed" error.

Since I have multiple projects under the same Solution, I would just rebuild the Project that I am currently working on. It appears that when this error happens that it would be better to rebuild whole Solution and resolve errors...

Jacquijacquie answered 19/11, 2020 at 22:49 Comment(0)
S
0

maybe it cause because of you did not add this in YOUR_PROJECT.csproj

<ItemGroup>

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.Dotnet" Version="2.0.2" />

Selfassurance answered 28/7, 2018 at 3:27 Comment(0)
D
0

Make sure you have code generation inside startup.cs. Also, make sure to use <TargetFramework> if you are using multiple target frameworks <TargetFrameworks>. You can temporary comment <TargetFrameworks> when running add-migration.

Also, if the PropertyGroup has Condition, you need to comment/remove them when running ef migration

Example

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
 <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.3" />
 </ItemGroup>

</Project>
Delegate answered 4/6, 2019 at 11:41 Comment(0)
C
0

I got the same error. I fixed it by stopping the project build. After that it worked fine.

Captainship answered 9/8, 2019 at 10:31 Comment(0)
D
0

Turns out this could also be caused by BuildEvents. For me I was referencing $(SolutionDir) there. With a regular build this variable has value, but running dotnet * commands is done at project level apparently. $(SolutionDir) comes out like Undefined in verbose mode (-v). This seems like a bug to me.

Diplex answered 5/12, 2019 at 9:33 Comment(0)
W
0

1: I was getting the error: PM> Add-Migration "InitialDB" Build failed. 2:The error was the "(" missing. I corrected that and error is gone.

so the error you can see the error in the output. The error was because of the code issue.

Walsingham answered 8/12, 2019 at 9:50 Comment(0)
G
0

In my case it was failing because an unrelated project in the solution was failing. Unloading that project and running the command again worked.

Genetic answered 16/1, 2020 at 3:17 Comment(0)
L
0

In my case it was because i had error somewhere.. Like missing coma in db config file, so VS didn't throw exception until build

So just try to run your code...

Lampley answered 17/1, 2020 at 9:2 Comment(1)
This does not provide an answer to the question. Please ensure that your answer improves upon other answers already present in this question.Jaredjarek
W
0

Same errors for me. I tried -v but nothing. Then I realised I had changed the model so much that the controller was showing errors.

Once I fixed the errors in the controller it worked.

Weever answered 10/5, 2020 at 4:55 Comment(0)
S
0

It might have many possibilities I guess. In my case, it was due packages versions inbalance

I had

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.3"/>

I just need it to downgrade the core Design package to 3.1.1 to match the upper core version

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1"/>

Then it worked fine

Study answered 3/7, 2020 at 4:2 Comment(0)
A
0

Only clearing Visual Studio cache helped me

  1. Close Visual Studio (ensure devenv.exe is not present in the Task Manager)
  2. Delete the %USERPROFILE%\AppData\Local\Microsoft\VisualStudio\16.0\ComponentModelCache directory.
  3. Restart Visual Studio. You could also need to cleanup your user's temp folder. It is usually located under %USERPROFILE%\AppData\Local\Temp.
Autotrophic answered 28/1, 2021 at 10:6 Comment(0)
M
0

I had the same problem. If you have opted .NET Core tool collect usage data for Windows improvement then you can use dotnet build to get the missing/persisting problem.

Monacid answered 3/3, 2021 at 16:48 Comment(0)
S
0

In my case it was sonarqube dependencies that were missing. When I built the project in Rider it succeeded but failed when triggered the build (dotnet build) from terminal. This was caused by a incorrect setup in Rider. The solution was to trigger a sonar scan in terminal.

Striped answered 7/4, 2021 at 7:50 Comment(0)
K
0

I faced the same error. The issue in my case was that I was running my backend server with the watch build and since .Net Build was overburdened, it was unable to execute the add migration command at the same time, therefore I had to stop running the application and then run the migration command again.

Kurtz answered 21/6, 2022 at 4:53 Comment(0)
C
0

Also, don't forget to check in what folder you are now in the package manager (PM) console. You can use "ls" command in windows to check the list of subfolders.

Coldiron answered 31/3, 2023 at 15:41 Comment(0)
L
0

In Visual Studio, open the Output window and select Build from Show output from. There you can see why the build failed.

I had a test project and it had an error. This is why the build failed when migrating. But my starting project which is the API layer had no error and was built successfully. So I conclude, EF Core Tools builds every project in the solution. Therefore it is better to check the Build output when a migration fails.

Louvre answered 6/1, 2024 at 5:46 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.