Windows 恢复后 Nuget 包不兼容和缺少命名空间

Nuget package incompatibility and missing namespaces after Windows restore

我的电脑最近决定死在我身上,所以在各种其他复苏方法都失败后,我决定恢复到 Window 的自动备份之一。备份是在我安装 Syncfusion Blazor 之前进行的,但那时我并没有遇到我目前遇到的错误。当我在 VS 中加载我的项目时,错误列表很快填满了如下消息:

Package Microsoft.AspNetCore.Components 3.1.4 is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1). Package Microsoft.AspNetCore.Components 3.1.4 does not support any target frameworks

'IServiceCollection' does not contain a definition for 'AddDbContext' and no accessible extension method 'AddDbContext' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'Syncfusion' could not be found (are you missing a using directive or an assembly reference?)

由于我 运行 使用与之前相同的代码,所以我不确定是什么导致了这些错误。我试过修复 VS、卸载和重新安装包以及“恢复 Nuget 包”选项。不幸的是,Intellisense 也没有帮助。

编辑: 这是我的 Startup.cs 文件:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ThinBlueLieB.Identity;
using ThinBlueLieB.Data;
using DataAccessLibrary.DataAccess;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Syncfusion.Blazor; //error- count not be found
using ThinBlueLieB.Helper;

namespace ThinBlueLieB
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<UserDbContext>(options => //error no definition
                options.UseMySql(Configuration.GetConnectionString("UserDB"), MySqlOptions => MySqlOptions
                .ServerVersion(new Version(8, 0, 18), ServerType.MySql)));

            services.AddDefaultIdentity<IdentityUser>(options => //error no definition
           options.SignIn.RequireConfirmedAccount = true)
                .AddEntityFrameworkStores<UserDbContext>();

            services.AddOptions();
            services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
            services.AddScoped<ISearches, Searches>();

            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
            services.AddSingleton<WeatherForecastService>();
            services.AddSingleton<IDataAccess, DataAccess>();            

            services.AddSyncfusionBlazor(); //error - no definition
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        { //error-no definition
            Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("MzA2NDMyQDMxMzgyZTMyMmUzMFVGc2cvTmU0d0NMbWZiRjBkZEl2WGlzU3lHdnBIcTNYRHYzYk5OSHRFTDA9");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage(); //error- no definition
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

重新安装 Visual Studio、删除所有软件包并在 CLI 中乱搞后,我找到了修复方法。虽然我不确定这是否适用于遇到此问题的每个人,但我所做的只是注释、构建和取消注释 csproj 文件中的所有 <PackageReference/> 标记。对于面临同样问题的其他人,如果这对您不起作用,请尝试:

  • Restoring packages
  • Repairing Visual Studio
  • 解决我的问题后,我收到一条通知,说之前的 csproj 和当前的 csproj 编码不同,因此请确保您的编码匹配。对我来说,修复后,它变成了UTF-8而不是西欧(Windows)