.NET 6 中缺少 Startup.cs 类

Her*_*cia 105 .net asp.net-core webapi .net-6.0

我在 .NET 5 中创建了 REST API,一切都运行良好,但最近我转移到 .NET 6 并意识到不存在startup.cs 类。由于没有startup.cs,如何在.NET 6 中添加数据库上下文?

小智 143

在 .NET 6 中,Microsoft 删除了 Startup.cs 类。只需转到program.cs 文件,您可以在其中添加连接字符串,然后您必须使用builder.Services.AddDbContext

老办法是

services.AddDbContext
Run Code Online (Sandbox Code Playgroud)

只需使用builder.Services,然后您就可以实现您想要的。

  • 如果需要,您仍然可以使用 Startup 类。您只需使用较旧的模板并升级,或者您可以手动创建以前样式的程序和启动类。 (4认同)
  • 构建器通过 'var builder = WebApplication.CreateBuilder(args);' 解析 稍后可用于配置 DbConetxt 、 AutoMapper 等。例如: builder.Services.AddDbContext<AppDbContext>(opt=>opt.UseInMemoryDatabase("InMem")); 执行解析为 IApplicationBuilder 的构建(var app = builder.Build();)后,该“app”用于调用 RUN / HTTPRedirection 等。 (2认同)

Arv*_*iya 56

在 .NET 6 中,它们统一Startup.csProgram.cs一个 Program.cs。

现在注册中间件、服务并将 DbContext 和其他所有内容添加到 Program.cs 文件中。

以下是 Program.cs 文件的示例:

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);
//Register services here
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(       
builder.Configuration.GetConnectionString("DefaultConnection")       
));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
//builder.Services.AddAuthentication(...)
var app = builder.Build();
//app.UseAuthentication();
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请访问此线程。


Emr*_*ner 24

创建一个名为“Startup.cs”的新类。将此模板粘贴到其中;

public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        this.Configuration = configuration;
    }

    // This method gets called by the runtime. Use this method to add serices to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        //services.AddDbContext...
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(WebApplication app, IWebHostEnvironment env)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以将 Program.cs 类修改为如下所示;

var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration); // My custom startup class.

startup.ConfigureServices(builder.Services); // Add services to the container.

var app = builder.Build();

startup.Configure(app, app.Environment); // Configure the HTTP request pipeline.

app.Run();
Run Code Online (Sandbox Code Playgroud)

现在,事情以旧的方式组织得更好。使用 ASP.NET Core 5 的人会有宾至如归的感觉。您在ConfigureServices() 和Configure() 中放入的内容将取决于您的项目和需求。例如,以下是作为起点的示例 ASP.NET Core 7 MVC 项目的 Startup.cs 的样子;

public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        this.Configuration = configuration;
    }

    // This method gets called by the runtime. Use this method to add serices to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(WebApplication app, IWebHostEnvironment env)
    {
        if (!env.IsDevelopment())
        {
            app.UseExceptionHandler("/Home/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.UseAuthorization();

        app.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*rew 8

对于那些尝试添加瞬态、单例或范围服务的人来说要清楚......

以前在Startup.cs中,添加服务如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IEmailService, EmailService>();
}
Run Code Online (Sandbox Code Playgroud)

现在在Program.cs中,它看起来像这样:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddTransient<IEmailService, EmailService>();
Run Code Online (Sandbox Code Playgroud)