Startup.cs file in ASP.NET MVC is not shown

Viewed 7929

enter image description hereHey I updated the VS2022 to last version and when I created a new MVC project I couldn't find startup file in soulation only the program.cs found? Is it normal? and how to add this code in program

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
5 Answers

In .Net 6.0 the program.cs and startup.cs have been unified. You can check the documentation in Migrate from ASP.NET Core 5.0 to 6.0

It is a similar process to insert your context just as you have done in startup.cs.

This answer is bit late but with the unified model, the dependency injection could be done at program.cs itself as shown below.

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DevConnection");

builder.Services.AddDbContext<StoresContext>(options => 
   options.UseSqlServer(connectionString));

Hey I know you said that you just used .NET 5.0 but for anyone who is using .NET 6.0 to run

services.AddDbContext<ApplicationDbContext>(options =>        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

All you need to do is add the following variable

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

And when you call the function

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));

And you should have no problem

Maybe, You have opened an empty new project. If you create new emot project, you must add startup.cs file

For me I added the services by using this code in Program.cs file :

builder.Services.AddSingleton<IHISInterface<Users> , UsersRepositiry>();
Related