簡體   English   中英

無法在 CLI 中從 Entity Framework Core model 生成 controller 和視圖

[英]Not able to generate controller and views from Entity Framework Core model in CLI

在 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 codeGeneratorName) 在 Microsoft.VisualStudio.Web.CodeGeneration. ] 參數)

我查看了很多文檔,所以是的,我已經看到了所有這些。 它沒有幫助我解決錯誤。 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator?view=aspnetcore-5.0

Github 問題上也有人建議將這兩個文件夾復制到 Ubuntu 上的 Templates 文件夾中,我也在 Windows 10 計算機上嘗試過。

  • 控制器生成器
  • 視圖生成器

但我仍然得到同樣的錯誤。 這是我正在使用的模型。

成分.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; }
        }
    }    

食譜.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;}    
        }
    }

配方成分.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 中右鍵單擊 controller 並添加“新腳手架項目”也不起作用。 不斷收到錯誤“運行所選代碼生成器 package 恢復失敗時出錯。”

唯一的解決方法是將框架從 .NET Core 3.1 升級到最新版本 .NET 5 並將軟件包升級到 5.0.1,這非常簡單,如此處所示

您所做的只是將項目文件更改為此並升級您通過 Visual Studio 中的 package 管理器 GUI 安裝的每個 NuGet package。

如果你想學習做這一切,請在這里查看我的博客

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM