Home > Mobile >  SQL Kata - Dependency Injection with .NET 6
SQL Kata - Dependency Injection with .NET 6

Time:08-12

What is the updated recommended way to use dependency injection with SQL Kata in a.NET 6 web project?

The current SQL Kata documentation for this has not been updated for .Net 6. The older syntax in startup.cs is shown here: https://sqlkata.com/docs/execution/setup#aspnet-core-di-container

services.Add<QueryFactory>(() => {

    // In real life you may read the configuration dynamically
    var connection = new MySqlConnection(
        "Host=localhost;Port=3306;User=user;Password=secret;Database=Users;SslMode=None"
    );

    var compiler = new MySqlCompiler();

    return new QueryFactory(connection, compiler);

});

CodePudding user response:

In Program.cs

builder.Services.AddTransient<QueryFactory>((e) =>
{
    var connection = new MySqlConnection("Host=localhost;Port=3306;User=user;Password=secret;Database=Users;SslMode=None");
    var compiler = new MySqlServerCompiler();
    return new QueryFactory(connection, compiler);
});
  • Related