Actual Program.cs v Tutorial Program.cs

Dean Everhart 1,541 Reputation points
2022-12-12T18:12:54.67+00:00

Following Contoso Tutorial at: https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-6.0

The tutorial tells me to replace code in Program.cs file with code in the tutorial that is highlighted in green.

Problem:

Program.cs file does not look anything like what is depicted in tutorial.

Actual Program.cs Code

var builder = WebApplication.CreateBuilder(args);  
  
// Add services to the container.  
builder.Services.AddControllersWithViews();  
  
var app = builder.Build();  
  
// Configure the HTTP request pipeline.  
if (!app.Environment.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?}");  
  
app.Run();  

Program.cs Depicted in Tutorial

269783-untitled1.png

Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-12T19:29:11.867+00:00

    you created a .net 6 program which uses new min api rather than 5.0 as the tutorial was written for. while its pretty simple to convert the code, you might find it easier to create the project as 5.0. then before making any changes, upgrade to 6.0.

    or create a startup.cs and copy all the tutorial code to it and program.cs

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. SurferOnWww 4,631 Reputation points
    2022-12-13T06:09:35.027+00:00

    Try followings:

    var builder = WebApplication.CreateBuilder(args);  
      
    // Add services to the container.  
    builder.Services.AddControllersWithViews();  
      
    var app = builder.Build();  
      
    // Add this  
    CreateDbIfNotExists(app);  
      
    // Configure the HTTP request pipeline.  
    if (!app.Environment.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?}");  
      
    app.Run();  
      
    // Add this  
    static void CreateDbIfNotExists(IHost host)  
    {  
        using (var scope = host.Services.CreateScope())  
        {  
            var services = scope.ServiceProvider;  
            try  
            {  
                var context = services.GetRequiredService<SchoolContext>();  
                DbInitializer.Initialize(context);  
            }  
            catch (Exception ex)  
            {  
                var logger = services.GetRequiredService<ILogger<Program>>();  
                logger.LogError(ex, "An error occurred creating the DB.");  
            }  
        }  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.