Mohamed ElNady
Mohamed ElNady

Reputation: 322

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

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")));

Upvotes: 10

Views: 20167

Answers (5)

MarwanAbu
MarwanAbu

Reputation: 179

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

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

Upvotes: 0

Charles de M.
Charles de M.

Reputation: 693

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.

Upvotes: 19

Revathi Selvaraj
Revathi Selvaraj

Reputation: 51

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));

Upvotes: 5

Alejandra Samayoa
Alejandra Samayoa

Reputation: 21

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

Upvotes: 2

karagoz
karagoz

Reputation: 135

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

Upvotes: 0

Related Questions