Reputation: 322
Hey 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
Reputation: 179
For me I added the services by using this code in Program.cs file :
builder.Services.AddSingleton<IHISInterface<Users> , UsersRepositiry>();
Upvotes: 0
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
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
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
Reputation: 135
Maybe, You have opened an empty new project. If you create new emot project, you must add startup.cs file
Upvotes: 0