微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在 CLI 中从 Entity Framework Core 模型生成控制器和视图

如何解决无法在 CLI 中从 Entity Framework Core 模型生成控制器和视图

在 ASP.NET Core 3.1 中,我正在寻找一种使用实体框架脚手架引擎从我的模型在 Windows 中的命令行生成控制器和 CRUD 视图的方法。似乎每个人都在使用 Visual Studio 在教程中做到这一点,就像这是唯一的方法。没有人在使用 VS Code。在 Windows 10 上不可能吗?

我使用这个命令。

dotnet aspnet-codegenerator controller -name RecipeIngredientsController -m RecipeIngredient -dc CookingContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

每次我尝试这样做时,我都会返回相同的错误

正在查找生成器“控制器”...未找到任何代码生成名称“控制器”在 Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String 代码生成名称)在 Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] 参数)

我看过很多文档,所以是的,我已经看到了所有这些。它没有帮助我解决错误https://docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator?view=aspnetcore-5.0

也有人在 Github 问题上建议将这 2 个文件夹复制到 Ubuntu 上的 Templates 文件夹,我也在我的 Windows 10 计算机上尝试过。

但我仍然遇到同样的错误。这是我正在使用的模型。

Ingredient.cs

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    namespace DTEditorLeftJoinSample.Models
    {
        public class Ingredient
        {
            public int ID {get;set;}
            [display(Name = "Ingredient Name")]
            public string IngredientName {get;set;}

            public ICollection<RecipeIngredient> RecipeIngredient { get; set; }
        }
    }    

Recipe.cs

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace DTEditorLeftJoinSample.Models
    {
        public class Recipe
        {
            public int ID { get; set; } 
            
            public string Title {get;set;}
            public string Descriptions {get;set;}
            public string Directions {get;set;} 
    
            public ICollection<RecipeIngredient> RecipeIngredient {get;set;}    
        }
    }

RecipeIngredient.cs

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DTEditorLeftJoinSample.Models
{
    public class RecipeIngredient
    {
        public int ID {get;set;}

        [display(Name = "Recipe ID")]
        public int RecipeID { get; set; }

        [display(Name = "Ingredient ID")]
        public int IngredientID { get; set; }
    
        public int quantity {get;set;}        
        public Recipe Recipe {get;set;}
        public Ingredient Ingredient {get;set;}
    }
}

解决方法

即使在 Visual Studio 中右键单击控制器并添加“新脚手架项”也不起作用。一直收到错误“运行所选代码生成器包还原失败时出错。”

唯一的解决方法是将框架从 .NET Core 3.1 升级到最新版本的 .NET 5,并将包升级到 5.0.1,这非常简单,如图here

您所做的就是将项目文件更改为此,并升级您通过 Visual Studio 中的包管理器 GUI 安装的每个 NuGet 包。

如果您想学习这一切,请参阅my blog here

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

  <PropertyGroup>
    <TargetFramework>netcoreapp5.0</TargetFramework>
    <AddRazorSupportForMvc>true</AddRazorSupportForMvc>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="DataTables-Editor-Server" Version="1.9.5" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.0" />    
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
  </ItemGroup>
</Project>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。