I work on asp.net core 2.2 vs2017 app I face issue I cannot get connection string from start up to static Data access class
class CZConnection
public class CZConnection
{
public string DashboardConnection { get; set; }
}
StartUp.cs
services.Configure<CZConnection>(DBConnection =>
{
DBConnection.DashboardConnection = Configuration.GetConnectionString("DashBoardSQLConnection");
});
public static partial class DataAccess
{
static SqlConnection InitializeConnection()
{
return new SqlConnection(here I need to get connection string of DBConnection.DashboardConnection);
}
return new SqlConnection();
}
The fact that you are even trying to use a static class in .NET Core is questionable. Static class causes 'static clng' that results in tightly coupled code and not clean coding.
You could look into using the info in the link. As long as you can access the appsetting.json, you don't need to have your data access class in a classlib project.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
public partial class DataAccess
{
public SqlConnection InitializeConnection()
{
return new SqlConnection(here I need to get connection string of DBConnection.DashboardConnection);
}
return new SqlConnection();
}
How to get connection to public class as above please ?
You need to do it by injecting it to the controller through Dependency Injection. Check the tutorial link I gave you.
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
the preferred approach is to inject the configuration:
public class DataAcessOptions
{
public string ConnectionString {get; set;}
// other options
}
public partial class DataAccess
{
private DataAcessOptions _options;
public DataAccess(DataAcessOptions options)
{
_options = options;
}
public Task<SqlConnection> InitializeConnection()
{
}
}
also for asp.net core, you should always use async sql operations.
this require register as service on startup meaning must create interface for service on startup.cs or no need
startup.cs
addscroped<idataaccess,dataaccess>
or no need
I am getting the connectionstring and passing DI all the way through the classes to where I need the connectionstring, by using Microsoft.Extensions.Options;
Member
39 Points
360 Posts
How to get connection from startup to static class Data access?
Jun 03, 2020 02:36 AM|ahmedbarbary|LINK
I work on asp.net core 2.2 vs2017 app I face issue I cannot get connection string from start up to static Data access class
Contributor
4933 Points
4205 Posts
Re: How to get connection from startup to static class Data access?
Jun 03, 2020 04:24 AM|DA924|LINK
The fact that you are even trying to use a static class in .NET Core is questionable. Static class causes 'static clng' that results in tightly coupled code and not clean coding.
https://objcsharp.wordpress.com/2013/07/08/why-static-code-is-bad/
https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection
You could look into using the info in the link. As long as you can access the appsetting.json, you don't need to have your data access class in a classlib project.
https://corderoski.wordpress.com/2017/09/18/how-to-read-appsettings-json-from-class-library-in-asp-net-core/
Participant
1253 Points
935 Posts
Re: How to get connection from startup to static class Data access?
Jun 03, 2020 05:52 AM|yogyogi|LINK
You can put the connection string in appsetttings.json and then do it's configuration in ConfigureServices() method of Startup.cs:
Kindly refer this tutorial for help.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Member
39 Points
360 Posts
Re: How to get connection from startup to static class Data access?
Jun 03, 2020 07:00 AM|ahmedbarbary|LINK
OK I need way to pass connection to class public
suppose I have class as below
How to get connection to public class as above please ?
Participant
1253 Points
935 Posts
Re: How to get connection from startup to static class Data access?
Jun 03, 2020 09:02 AM|yogyogi|LINK
You need to do it by injecting it to the controller through Dependency Injection. Check the tutorial link I gave you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
All-Star
58184 Points
15655 Posts
Re: How to get connection from startup to static class Data access?
Jun 03, 2020 03:30 PM|bruce (sqlwork.com)|LINK
the preferred approach is to inject the configuration:
also for asp.net core, you should always use async sql operations.
Member
39 Points
360 Posts
Re: How to get connection from startup to static class Data access?
Jun 03, 2020 11:13 PM|ahmedbarbary|LINK
thank you for reply
this require register as service on startup meaning must create interface for service on startup.cs or no need
startup.cs
addscroped<idataaccess,dataaccess>
or no need
Contributor
4933 Points
4205 Posts
Re: How to get connection from startup to static class Data access?
Jun 04, 2020 01:39 AM|DA924|LINK
I am getting the connectionstring and passing DI all the way through the classes to where I need the connectionstring, by using Microsoft.Extensions.Options;
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1
I gave you the link on how to do it in my first post.
In startup.cs Configuration service method