“Build failed” on Database First Scaffold-DbContext

后端 未结 18 1553
醉梦人生
醉梦人生 2020-12-24 04:31

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: ht

相关标签:
18条回答
  • 2020-12-24 05:00

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

    0 讨论(0)
  • 2020-12-24 05:02

    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://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

    0 讨论(0)
  • 2020-12-24 05:02

    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>
    
    0 讨论(0)
  • 2020-12-24 05:07

    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
    0 讨论(0)
  • 2020-12-24 05:07

    This stopped working for me today. So I tried running the dotnet scaffold command from the command line and it worked first time. Don't ask me!!

    0 讨论(0)
  • 2020-12-24 05:08

    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.

    0 讨论(0)
提交回复
热议问题