2

I have established a MySql connection in .NET 5.0 in startup.cs, but would like to do almost the exact same thing in a new MVC web app using .NET 6.0, and startup.cs is no longer a part of .NET 6.0. Below is the connection I used before, written in C#. How would I do the same thing in program.cs in .NET 6.0?

Code from .NET 5.0:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IDbConnection>((s) =>
            {
                IDbConnection conn = new MySqlConnection(Configuration.GetConnectionString("behs"));
                conn.Open();
                return conn;
            });

            services.AddTransient<IProductRepository, ProductRepository>();

            services.AddControllersWithViews();
        }

I know (i.e. strongly suspect) it is done using builder, but cannot quite get it to work.

Again, I suspect the solution lies in program.cs, somewhere here:

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

var app = builder.Build();

Any and all help is appreciated, thanks!

0 Answers0