You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行dotnet ef迁移命令遇Build Failed错误,.NET Core 2.2环境求助

.NET Core 2.2执行EF迁移命令时Build Failed问题求助
  • 执行dotnet ef migrations add InitialCreate命令时出现Build Failed错误
  • 运行dotnet build会出现黄色警告,但项目可以正常执行dotnet run
  • 项目必须使用.NET Core SDK 2.2(新版本SDK无Startup.cs结构),无法升级到更高版本
  • 已尝试执行dotnet ef database update命令,问题仍未解决

求解决这个错误的方法。

Startup.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using rentapi.Data;
using Microsoft.EntityFrameworkCore;

namespace rentapi
{
    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.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddDbContextPool<DataContext>(options=> options.UseSqlServer(Configuration.GetConnectionString("DBCS")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // 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.UseMvc();
        }
    }
}

DataContext.cs文件

using rentapi.Models;
using Microsoft.EntityFrameworkCore;

namespace rentapi.Data
{
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options):base(options){}


        public DbSet<Employee> Employees { get; set; }
    }
}

appsettings.json文件

{
  "ConnectionStrings": {
    "DBCS":"Server=.; Database=rent; Trusted_Connection=True;"
  },

  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

内容的提问来源于stack exchange,提问作者prawlercode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:02:49