【问题标题】:Visual Studios Fails every time trying to scaffold controller with views每次尝试使用视图搭建控制器时,Visual Studios 都会失败
【发布时间】:2022-07-02 15:15:48
【问题描述】:

我有一个 .Net core 5 项目,我已经完成了大约 2/3 的工作。我必须在数据库中添加一个新表来跟踪客户的附加表单。我现在已经添加了该表的模型并将其添加到我的上下文中:

型号:

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

#nullable disable

namespace EnvApp.Models.DB
{
    [Table("Type_Two")]
    public class TypeTwo
    {
        [Key]
        public long ID { get; set; }
        [MaxLength(50), Required]
        public string State_Project_Number { get; set; }
        [MaxLength(50)]
        public string Federal_Project_Number { get; set; }
        [MaxLength(50)]
        public string Name { get; set; }
        [MaxLength(50)]
        public string Route_Number { get; set; }
        public string County { get; set; }
        [Required]
        public string Category { get; set; }
        public int ADT_Existing { get; set; }
        public int ADT_Projected { get; set; }
        [MaxLength(100)]
        public string Coordinates { get; set; }
        [Required]
        public bool Federal_Nexus { get; set; }
        public string Existing_Conditions { get; set; }
        public string Preferred_Alternatives { get; set; }
        public string Other_Alternatives { get; set; }
        public string Public_Involvement { get; set; }
        public bool Right_of_Way { get; set; }
        [MaxLength(100)]
        public string Right_of_Way_Comments { get; set; }
        public bool Federal_Land { get; set; }
        [MaxLength(100)]
        public string Federal_Land_Comments { get; set; }
        public bool Maintenance_of_Traffic { get; set; }
        [MaxLength(100)]
        public string Maintenance_of_Traffic_Comment { get; set; }
        public bool Public_Controversy { get; set; }
        [MaxLength(100)]
        public string Public_Controversy_Comment { get; set; }
        public bool History { get; set; }
        [MaxLength(100)]
        public string History_Comment { get; set; }
        public bool H_Adverse_Effect { get; set; }
        [MaxLength(100)]
        public string H_Adverse_Effect_Comment { get; set; }
        public bool Archaeology { get; set; }
        [MaxLength(100)]
        public string Archaeology_Comment { get; set; }
        public bool A_Adverse_Effect { get; set; }
        [MaxLength(100)]
        public string A_Adverse_Effect_Comment { get; set; }
        public bool US_Forest_Service { get; set; }
        [MaxLength(100)]
        public string US_Forest_Service_Comment { get; set; }
        public bool US_Army_Corps_Eng { get; set; }
        [MaxLength(100)]
        public string US_Army_Corps_Eng_Comment { get; set; }
        public bool National_Park_Service { get; set; }
        [MaxLength(100)]
        public string National_Park_Service_Comment { get; set; }
        public bool Wild_Scenic_River { get; set; }
        [MaxLength(100)]
        public string Wild_Scenic_River_Comment { get; set; }
        public bool National_River { get; set; }
        [MaxLength(100)]
        public string National_River_Comment { get; set; }
        public bool National_Wildlife_Refuge { get; set; }
        [MaxLength(100)]
        public string National_Wildlife_Refuge_Comment { get; set; }
        public bool State_Park { get; set; }
        [MaxLength(100)]
        public string State_Park_Comment { get; set; }
        public bool State_Wildlife_Management_Area { get; set; }
        [MaxLength(100)]
        public string State_Wildlife_Management_Area_Comment { get; set; }
        public bool Other_Park { get; set; }
        [MaxLength(100)]
        public string Other_Park_Comment { get; set; }
        public bool Historic_Property { get; set; }
        [MaxLength(100)]
        public string Historic_Property_Comment { get; set; }
        public bool Park_Recreational { get; set; }
        [MaxLength(100)]
        public string Park_Recreational_Comment { get; set; }
        public bool DE_Minimis { get; set; }
        [MaxLength(100)]
        public string DE_Minimis_Comment { get; set; }
        public bool Programmatic { get; set; }
        [MaxLength(100)]
        public string Prog_Comment { get; set; }
        public bool D_Individual { get; set; }
        [MaxLength(100)]
        public string D_Individual_Comment { get; set; }
        public bool LWCFA { get; set; }
        [MaxLength(100)]
        public string LWCFA_Comment { get; set; }
        public bool Temp_Change { get; set; }
        [MaxLength(100)]
        public string Temp_Change_Comment { get; set; }
        public bool Conversion_of_Property { get; set; }
        [MaxLength(100)]
        public string Conversion_of_Property_Comment { get; set; }
        public bool US_Fish_Wildlife { get; set; }
        [MaxLength(100)]
        public string US_Fish_Wildlife_Comment { get; set; }
        public bool Formal_Consultation { get; set; }
        [MaxLength(100)]
        public string Formal_Consultation_Comment { get; set; }
        public bool Division_Natural_Resources { get; set; }
        [MaxLength(100)]
        public string Division_Natural_Resources_Comment { get; set; }
        public bool Floodplain_Enchroachment { get; set; }
        [MaxLength(100)]
        public string Floodplain_Enchroachment_Comment { get; set; }
        public bool Farmland_Involvment { get; set; }
        [MaxLength(100)]
        public string Farmland_Involvment_Comment { get; set; }
        public bool FourZeroFour { get; set; }
        [MaxLength(100)]
        public string FourZeroFour_Comment { get; set; }
        public bool Nationwide { get; set; }
        [MaxLength(100)]
        public string Nationwide_Comment { get; set; }
        public bool F_Individual { get; set; }
        [MaxLength(100)]
        public string F_Individual_Comment { get; set; }
        public bool USCG_Sec_9 { get; set; }
        [MaxLength(100)]
        public string USCG_Sec_9_Comment { get; set; }
        public bool USCG_Sec_10 { get; set; }
        [MaxLength(100)]
        public string USCG_Sec_10_Comment { get; set; }
        public bool Fed_Special_Use_Permit { get; set; }
        [MaxLength(100)]
        public string Fed_Special_Use_Permit_Comment { get; set; }
        public bool Noise { get; set; }
        [MaxLength(100)]
        public string Noise_Comment { get; set; }
        public bool Air_Quality { get; set; }
        [MaxLength(100)]
        public string Air_Quality_Comment { get; set; }
        public bool Haz_Waste { get; set; }
        [MaxLength(100)]
        public string Haz_Waste_Comment { get; set; }
        public bool Airport { get; set; }
        [MaxLength(100)]
        public string Airport_Comment { get; set; }
        public bool Mussel_Survey { get; set; }
        [MaxLength(100)]
        public string Mussel_Survey_Comment { get; set; }
        public bool Mussel_Relocation { get; set; }
        [MaxLength(100)]
        public string Mussel_Relocation_Comment { get; set; }
        public bool WVDOH_Special_Prov { get; set; }
        [MaxLength(100)]
        public string WVDOH_Special_Prov_Comment { get; set; }
        [MaxLength(20)]
        public string Perepared_By { get; set; }
        [MaxLength(20)]
        public string WV_DOH_Approved_By { get; set; }
        [Required]
        public bool FHWA_Approval { get; set; }
        [MaxLength(20)]
        public string FHWA_Approved_By { get; set; }
        [MaxLength(20)]
        public string Add_User { get; set; }
        public DateTime Date_Added { get; set; }
    }
}

上下文:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace EnvApp.Models.DB
{
    public partial class Natural_ResourcesContext : DbContext
    {
        public Natural_ResourcesContext()
        {
        }

        public Natural_ResourcesContext(DbContextOptions<Natural_ResourcesContext> options)
            : base(options)
        {
        }

        public virtual DbSet<NR_User> NR_Users { get; set; }
        public virtual DbSet<Project_Screen> Project_Screen { get; set; }
        public virtual DbSet<TypeOne> TypeOne { get; set; }
        public virtual DbSet<TypeTwo> TypeTwo { get; set; }
    } 
}

当我尝试使用 TypeTwo 模型的视图构建控制器时,我收到一条错误消息:

Error
There was an error running the selected code generator: 'Package restore failed. Rolling back package changes for 'EnvApp'.'

这就是我得到的所有信息。

如何获得更多信息来诊断问题?如果这是一个常见问题,那么解决方法是什么?

如果我尝试更新 NuGet 包管理器解决方案中的所有包,我会收到以下错误消息:

Restoring packages for C:\Users\e096752\Documents\Cole's Git Repos\ELCC\EnvApp\EnvApp.csproj...
NU1701: Package 'ExpressiveAnnotations 2.9.6' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project.
NU1701: Package 'Newtonsoft.Json 4.5.11' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project.
NU1202: Package Microsoft.EntityFrameworkCore.SqlServer 6.0.1 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package Microsoft.EntityFrameworkCore.SqlServer 6.0.1 supports: net6.0 (.NETCoreApp,Version=v6.0)
NU1202: Package Microsoft.EntityFrameworkCore.Design 6.0.1 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package Microsoft.EntityFrameworkCore.Design 6.0.1 supports: net6.0 (.NETCoreApp,Version=v6.0)
NU1202: Package Microsoft.EntityFrameworkCore.Tools 6.0.1 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package Microsoft.EntityFrameworkCore.Tools 6.0.1 supports: net6.0 (.NETCoreApp,Version=v6.0)
NU1202: Package Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.1 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Package Microsoft.VisualStudio.Web.CodeGeneration.Design 6.0.1 supports: net6.0 (.NETCoreApp,Version=v6.0)
Package restore failed. Rolling back package changes for 'EnvApp'.

【问题讨论】:

  • 这确实有帮助,我今天早上早些时候就有了这个想法。唯一的问题是,当我尝试更新所有内容时,我收到了我在更新的问题中列出的错误消息。知道如何解决这个问题吗?
  • 更新 Newtonsoft.Json 包到兼容版本,为 ExpressiveAnnotations 寻找替代方案!并设置应用程序---->目标框架---->.NET 5.0

标签: c# asp.net asp.net-mvc entity-framework asp.net-core


【解决方案1】:

我有这个问题并且搜索了很多关于这个。 这个解决方案可能会有所帮助: 您可以执行以下步骤:

1- VS 工具

2- 选项

3- Nuget 包管理器

4- 一般

5- 清除所有 Nuget 缓存。

您需要更改目标 .net 核心,如果您将 VS 更新为最新版本(例如 VS 2017 15.3),这是可能的

这个reference也可以帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多