1

I keep getting this error when I try to Add Razor Pages with Entity Framework Core (CRUD):

enter image description here

How do I go about resolving something like this?

Here is my .csproj file

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <StartupObject>BCC_PurchasingAuth.Program</StartupObject>
    <ApplicationIcon></ApplicationIcon>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Ardalis.GuardClauses" Version="3.0.1" />
    <PackageReference Include="AutoMapper.Collection.EntityFrameworkCore" Version="7.0.1" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\BCC_PurchasingAuthDB\BCC_PurchasingAuthDB.csproj" />
  </ItemGroup>

</Project>

These are the resources I've looked at:

"Build failed" on Database First Scaffold-DbContext

Can't Add View from Controller in VS 2015 : "There was an error running the selected code generator"

https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-5.0&tabs=visual-studio

Full disclosure: my solution consists of two projects. I used EF Core Power Tools to reverse engineer my database into a class library project. And my pages are in a separate application project.

Here is the .csproj file for the class library:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" />
  </ItemGroup>

</Project>

Let me know what other information is useful for this type of error and I'll add it.

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
SpookyBooky
  • 129
  • 2
  • 9

1 Answers1

1

You need to change your class library.csproj file to following

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

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.11">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
</ItemGroup>

And your .csproj file:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <StartupObject>BCC_PurchasingAuth.Program</StartupObject>
    <ApplicationIcon></ApplicationIcon>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Ardalis.GuardClauses" Version="3.0.1" />
    <PackageReference Include="AutoMapper.Collection.EntityFrameworkCore" Version="7.0.1" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.11">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\BCC_PurchasingAuthDB\BCC_PurchasingAuthDB.csproj" />
  </ItemGroup>

</Project>
Yinqiu
  • 6,609
  • 1
  • 6
  • 14
  • Hi, we're having a hard time understanding why this is the solution. Would you be able to describe the required changes more clearly? – cwahls Feb 06 '21 at 06:29
  • 1
    Change your version of efcore from 5.0 to 3.x – Yinqiu Feb 06 '21 at 06:31
  • 1
    Because op's project version is 3.0 – Yinqiu Feb 06 '21 at 06:32
  • What would I need to do in order to keep the latest version of ef core? – SpookyBooky Feb 06 '21 at 22:17
  • Hi @SpookyBooky,Can you successfully generate the scaffold now? If you want to keep the latest version of efcore, you need to upgrade your project to .net 5. – Yinqiu Feb 07 '21 at 00:35
  • Hi @SpookyBooky,If my answer is helpful,can you [accept it as answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)?Thanks:) – Yinqiu Feb 07 '21 at 01:29
  • Good morning, I can’t check until I go back to the office tomorrow. I will accept it as son as I know if it works. Thanks for the replies. – SpookyBooky Feb 07 '21 at 14:24