Add migration initialcreate ошибка

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

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.

Leniel Maccaferri's user avatar

asked Jun 27, 2017 at 16:33

jceddy's user avatar

8

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.

Vivek Nuna's user avatar

Vivek Nuna

23.1k18 gold badges95 silver badges181 bronze badges

answered Sep 4, 2017 at 16:06

mmh18's user avatar

mmh18mmh18

1,5232 gold badges11 silver badges11 bronze badges

3

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

Fabala Dibbasey's user avatar

answered Dec 12, 2017 at 15:19

Makla's user avatar

MaklaMakla

9,61115 gold badges69 silver badges138 bronze badges

3

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.

answered Mar 17, 2020 at 17:29

Leniel Maccaferri's user avatar

Leniel MaccaferriLeniel Maccaferri

99k45 gold badges363 silver badges468 bronze badges

1

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.

answered Apr 12, 2019 at 8:49

Romain Vergnory's user avatar

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.

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered Nov 26, 2018 at 13:23

Bharat chaudhari's user avatar

0

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.

answered Apr 21, 2018 at 21:09

Liam Kernighan's user avatar

Liam KernighanLiam Kernighan

2,2471 gold badge21 silver badges23 bronze badges

1

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

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered May 9, 2019 at 18:08

Denis Nutiu's user avatar

Denis NutiuDenis Nutiu

94814 silver badges21 bronze badges

2

Had the same situation.
This helped:

  1. Open Package Manager Console

  2. Change directory to Migrations folder:

    cd C:YourSolutionYourProjectWithMigrations
    
  3. Enter in PM:

    dotnet ef migrations add YourMigrationName
    

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered Feb 2, 2018 at 9:15

Yaroslav's user avatar

YaroslavYaroslav

4746 silver badges13 bronze badges

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.

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered Jun 28, 2018 at 8:28

Archeg's user avatar

ArchegArcheg

8,2926 gold badges41 silver badges88 bronze badges

2

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.

Fourat's user avatar

Fourat

2,3262 gold badges40 silver badges53 bronze badges

answered Dec 8, 2019 at 17:29

Sara Onval's user avatar

Sara OnvalSara Onval

1141 silver badge4 bronze badges

1

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.

answered Jul 30, 2018 at 10:53

Andy Parinas's user avatar

0

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.

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered Apr 25, 2019 at 14:58

andrescpacheco's user avatar

andrescpachecoandrescpacheco

5341 gold badge7 silver badges24 bronze badges

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.

answered Jun 29, 2017 at 22:59

David Liang's user avatar

David LiangDavid Liang

19.7k6 gold badges42 silver badges67 bronze badges

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

answered Apr 19, 2018 at 15:40

ec2011's user avatar

ec2011ec2011

5706 silver badges20 bronze badges

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

answered Aug 20, 2020 at 15:03

Keti Kutateladze's user avatar

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

Pang's user avatar

Pang

9,344146 gold badges85 silver badges121 bronze badges

answered Oct 4, 2018 at 20:12

davut temel's user avatar

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

answered Jun 27, 2017 at 21:38

jceddy's user avatar

jceddyjceddy

1,5972 gold badges11 silver badges18 bronze badges

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

answered Jun 29, 2018 at 10:24

gbarel's user avatar

gbarelgbarel

711 silver badge8 bronze badges

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.

answered Aug 27, 2018 at 17:23

ahmet bilgic's user avatar

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

answered Aug 16, 2019 at 11:54

Imran Sh's user avatar

Imran ShImran Sh

1,4934 gold badges30 silver badges46 bronze badges

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.

answered Dec 20, 2019 at 10:51

Khorram Bin Salim Khondkar's user avatar

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)

answered Apr 29, 2020 at 16:19

pamal Sahan's user avatar

pamal Sahanpamal Sahan

4213 silver badges7 bronze badges

In my case I had couple of such errors:

The process cannot access the file 'C:xxxbinDebugnetcoreapp3.1yyy.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.

answered Aug 27, 2020 at 13:00

infografnet's user avatar

infografnetinfografnet

3,5891 gold badge33 silver badges34 bronze badges

1

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.

answered Jun 21, 2022 at 4:53

Dinindu Gunathilaka's user avatar

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" />

answered Jul 28, 2018 at 3:27

Farshad's user avatar

Make sure you have code generation inside startup.cs

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>

answered Jun 4, 2019 at 11:41

TAHA SULTAN TEMURI's user avatar

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

answered Aug 9, 2019 at 10:31

lrefopam's user avatar

lrefopamlrefopam

4711 gold badge4 silver badges20 bronze badges

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.

answered Dec 5, 2019 at 9:33

petko's user avatar

petkopetko

1431 silver badge13 bronze badges

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.

answered Dec 8, 2019 at 9:50

Mani's user avatar

ManiMani

3272 silver badges10 bronze badges

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

answered Jan 16, 2020 at 3:17

Pranav Raj's user avatar

Pranav RajPranav Raj

7411 gold badge8 silver badges19 bronze badges

RRS feed

  • Remove From My Forums
  • Question

  • User887623398 posted

    Hi

    I run this command on package manager console :

    pm>add-migration FirstAppDemo

    but keeping <g class=»gr_ gr_100 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace» id=»100″ data-gr-id=»100″>get</g> this image:

    The project first app demo failed to build.

    What should I do?

    please inform.

    regards

    Saeed

All replies

  • User887623398 posted

    Thanks for your reply.

    I’m afraid my entity framework and sql configuration is correct in start up.cs.

    Here comes my configuration:

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
                
                var sqlconnection = "Data Source=(localdb)\mssqllocaldb;Initial Catalog=FirstAppDemo";
                services.AddDbContext<FirstAppDemoDbContext>(dbcontextoption => dbcontextoption.UseSqlServer(sqlconnection));
    
    
    
            }

    Would you please let me know if any thing is wrong?

  • User-369506445 posted

    It’s not depended to your config

    According to your error, I said first build your project and sure you do not get any error

    If your project become build success

    Then 

    • Run the Enable-Migrations command in Package Manager Console . (make sure that default project is the project where your context class is)

    Next Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was
    created

  • User887623398 posted

    I built my project and there is no error.

    In <g class=»gr_ gr_20 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace» id=»20″ data-gr-id=»20″>microsoft</g> documentation comes to this command:

    Add-Migration InitialCreate

    and when I write and run this command after a while this error comes which is highlighted:

    Object reference not set to an instance of an object.

    please inform.
     

  • User1528429020 posted

    Having the same problem, if you found any solution please help

    PM> add-migration AddBooleanPurposeColumnsToCustomersTable
    The project ‘CustomersDemo’ failed to build.

After installing the following packages:

"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final" (-Pre)

When running the add-migration "InitialCreate" command, I get the following error:

Cannot execute this command because Microsoft.EntityFrameworkCore.Design is not installed. Install the version of that package that matches the installed version of Microsoft.EntityFrameworkCore and try again. Though, if you look at the .json file below, you would see that the packages are matching, so this error message is contradicting to me.

This is my project.json file:

{
  "runtimes": {
    "win10-x64": {}
  },

  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
} 

After installing the following packages:

"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final" (-Pre)

When running the add-migration "InitialCreate" command, I get the following error:

Cannot execute this command because Microsoft.EntityFrameworkCore.Design is not installed. Install the version of that package that matches the installed version of Microsoft.EntityFrameworkCore and try again. Though, if you look at the .json file below, you would see that the packages are matching, so this error message is contradicting to me.

This is my project.json file:

{
  "runtimes": {
    "win10-x64": {}
  },

  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
} 

Solution: Add-Migration Build Failed. Update-Database Failed

Add-Migration initial -v Build started Build failed or Update-Database Failed

Solution:

  1. Make sure the solution is building right, clean and rebuild, if any project fails, fix it.
  2. Use dot net tool through command prompt to add the migration it will give you clear information
dotnet ef migrations add MigrationName -v

Now fix the issue, in my case I needed to add reference to Microsoft.EntityFrameworkCore.Design

So I installed Microsoft.EntityFrameworkCore.Tools and it fixed the issue.

if still looking for answers check this post

Happy Coding!

Published by scientistz

Zaheer is Microsoft CertifiedTrainer MCT. He is an azure solutions architect along with SharePoint Developer/Admin having years of Microsoft products experience essentially in development. He is also Microsoft Certified Azure Solution Architect Expert(MCSE) and Microsoft Certified Solution Developer App Builder(MCSD). He loves to help others and make a change in the world and this blog is the one of ways, He uses to influence people.
View all posts by scientistz

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Adb tcpip 5555 error no devices emulators found
  • Adb sideload error closed
  • Adb shell ошибка
  • Adb shell error no devices emulators found
  • Adb shell error closed

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии