1

How can I point all generated dataset schema connection string to use appsettings default connection string in ASP.NET Core Razor pages?

Note: this works perfectly in ASP.NET Webforms and ASP.NET MVC, but not in ASP.NET Core with Razor pages.

Generated code from dataset Xsd Schema:

private void InitConnection()
{
    this._connection = new global::System.Data.SqlClient.SqlConnection();
    this._connection.ConnectionString = "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=mylocaldb;Integrated Security=True";
}

I want the connection string to point to the appsettings.json default connection.

Thanks

Please click the link to view the Image: How can I override the InitConnection() at runtime. Note: this is actually a generated code by visual studio and everytime I update the dataset it will automatically reset back to default so I can't use change the code.

Please click to view the Image

MB_18
  • 1,620
  • 23
  • 37

2 Answers2

0

Below code in dotnet core should be same as yours.

services.AddScoped<IDbConnection, SqlConnection>(serviceProvider => {
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = 
             Configuration.GetConnectionString("Connectionstring");
    return conn;
});

For more details, you can refer below sample code:

How do I handle Database Connections with Dapper in .NET?

Rena
  • 30,832
  • 6
  • 37
  • 72
Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • Thank you, much appreciated. The challenge I have here is the xsd codes are generated codes (Datasets.Designer.cs) If I change the InitConnection() to point to the appSettings, Everytime I add/Update the Dataset schema (Xsd), it will revert back to default connectionstring This._connection.ConnectionString = "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=mylocaldb;Integrated Security=True"; – EdupowerBox Dec 29 '21 at 21:00
  • I have updated the question thanks – EdupowerBox Dec 29 '21 at 21:18
0

Solution 1: (The simple way)

You could useIConfiguration interface. In the bellow code, I inject theIConfiguration interface to your code and use it.

Here is the code:

public class MyClass
{
   private  IConfiguration _configuration { get;}
    
   public MyClass(IConfiguration configuration)
   {
       _configuration = configuration;
   }

   private void InitConnection()
   {
       this._connection = new global::System.Data.SqlClient.SqlConnection();
       this._connection.ConnectionString = _configuration.GetConnectionString("DefaultConnection");
   }
}

and here is theappsettings.json file:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=mylocaldb;Integrated Security=True"
  },
  "AllowedHosts": "*"
}

Solution 2: (The best way)

Add a service in your startup.cs file and inject the service to your classes.

MB_18
  • 1,620
  • 23
  • 37
  • Thank you, much appreciated. The challenge I have here is the xsd codes are generated codes (Datasets.Designer.cs) If I change the InitConnection() to point to the appSettings, Everytime I add/Update the Dataset schema (Xsd), it will revert back to default connectionstring This._connection.ConnectionString = "Data Source=(localdb)\\mssqllocaldb;Initial Catalog=mylocaldb;Integrated Security=True"; – EdupowerBox Dec 29 '21 at 21:03
  • I have added image to the question thanks – EdupowerBox Dec 29 '21 at 21:15