.NET核心6.0:在Visual Studio 2022中添加新的scaffolded razor页面时出错?

wwodge7n  于 2023-03-09  发布在  .NET
关注(0)|答案(2)|浏览(293)

我一直在尝试使用实体框架(CRUD)在ASP.NET核心、Razor页面中添加一个scaffold项。我当前的目标框架是.NET6。
我有以下软件包版本:

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.7">
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.7" />

这是我正在遵循的说明:https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/model?view=aspnetcore-6.0&tabs=visual-studio
这是我得到的错误:

下面是我的模型:

using System.ComponentModel.DataAnnotations;

namespace RazorPagesMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; } = string.Empty;

        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; } = string.Empty;
        public decimal Price { get; set; }
    }
}
mwg9r5ms

mwg9r5ms1#

当您创建模型的搭建时,您添加了较少的Microsoft.EntityFrameworkCore.Design包,这是搭建工具所必需的。
软件包版本:

<PackageReference Include=" Microsoft.EntityFrameworkCore.Design " Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.7">
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.7" />
jobtbby3

jobtbby32#

我想知道这些依赖项的安装顺序是否会影响它们的行为。它不应该真的。但万一它...
官方文档建议可以使用软件包管理器(从 * 工具 * 菜单中选择 * NuGet软件包管理器 软件包管理器控制台 * 或PMC)按以下顺序将这些依赖项安装在您的项目中:

Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
Install-Package Microsoft.EntityFrameworkCore.Design
Install-Package Microsoft.EntityFrameworkCore.SqlServer

前面的命令添加:

  • EF Core SQL Server提供程序。提供程序包将EF Core包作为依赖项安装。
  • 在搭建步骤中自动安装的包所使用的实用程序。

图片来源:www.example.comhttps://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-7.0&tabs=visual-studio#add-nuget-packages
此外,这是安装后我的项目文件中的内容(在MyProject.csproj):

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

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.4" />
  </ItemGroup>

</Project>

相关问题