Read Entity Framework 6 Connection String from appsettings.json file in ASP.NET CORE - asp.net-core

I have a app.config xml file to save entity framework's connection strings as the following example:
<configuration>
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/EntityFramework.MyEntities.csdl|res://*/EntityFramework.MyEntities.ssdl|res://*/EntityFramework.MyEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Example;persist security info=True;user id=myId;password=myPassword;multipleactiveresultsets=false;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
But I'm using ASP.NET Core, I manage multiple servers with an enviroment variable. so I need to save this connection string in my appsettings.{enviroment}.json, read this connection and set to the property entity framework context.

I think you are looking for something like the following construction.
"ConnectionStrings": {
"MyEntities": "metadata=res://*/EntityFramework.MyEntities.csdl|res://*/EntityFramework.MyEntities.ssdl|res://*/EntityFramework.MyEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=myserver;initial catalog=Example;persist security info=True;user id=myId;password=myPassword;multipleactiveresultsets=false;application name=EntityFramework"
}

Not sure if this is the correct way to do this, but this may be a good starting point for you. I've added the configuration settings into the file named app.config. It appears that the contents in the files are written into the exe's config file. When the exe is executed the config settings are also loaded. I have just starting experimenting with .NET Core, so this may not be the recommended or correct way.
<configuration>
<runtime>
<gcServer enabled="true"/>
</runtime>
<connectionStrings>
<add name="TestName" connectionString="data source=MyServer;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>

Related

Sql connection string for another pc

I have an application that runs on SQL 2005 database
I m trying to use same application on another machine same network,
I managed to log in on SQL 2005 using IP
Now I need to configure applications app.config file (found in visual studio solution) to allow connection
soo far I tried this connection string which isn't worked
this is what my config file looks like
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=ipaddress,1433;Initial Catalog=sample;User ID=sample;Password=sample;Trusted_Connection=True;" />
</appSettings>
</configuration>
Any help would be appreciated
Add this to your config file. Rather then using an appSetting use the actual connectionStrings section.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=00.000.0.00,1433;Initial Catalog=taxi;Persist Security Info=True;User ID=sample;Password=sample; providerName="System.Data.SqlClient" />
</connectionStrings>
This string should work if anyone else got the same problem
thanks everyone for looking and trying to figure it out with me
<add key="ConnectionString" value="Data Source=00.00.000.000;Initial Catalog=sample;User ID=sample;Password=sample;" />

Retrieving default database connection string in code

I am working with the existing structures used by the dev team at my current company. In order to change connection strings within the application, the team creates an XML node called "defaultDatabase" (see below):
</configSections>
<dataConfiguration defaultDatabase="Development" />
<connectionStrings>
<add name="Development" connectionString="Data source=DVHQSQL01; Initial Catalog=db; User ID=id; Password=password"/>
</connectionStrings>
...there will be more connection strings in the XML for different server environments (not shown here, but "Testing", "Staging", "Production", etc)
I am creating a class with different functions that use the default connection. I know one route is through ConfigurationManager as shown here:
https://social.msdn.microsoft.com/Forums/en-US/9a8c9f5a-092e-4c4a-87bb-9f35d8f55da1/get-connection-string-from-appconfig-file?forum=adodotnetdataproviders
This is great if you can change the connection string name in code, but we use the defaultDatabase node to be able to change from different environments without rebuilding.
Does anyone have experience with this methodology? How do I get the correct connection string using the defaultDatabase node?
Just set the default connection in the connection strings area:
<connectionStrings>
<add name="default" connectionString="..."/>
</connectionStrings>
If you have further connections you can add them in the same way:
<connectionStrings>
<add name="default" connectionString="..."/>
<add name="special1" connectionString="..."/>
<add name="special2" connectionString="..."/>
</connectionStrings>
You can have as many connectionStrings as you want in your config, all you have to do is give them different names.
<connectionStrings>
<add name="default" connectionString=""/>
<add name="anotherOne" connectionString=""/>
...
</connectionStrings>

Why does my connection string not work?

I want to upload my website. I uploaded my database's backup file and the connection string that the host give me is:
Provider=sqloledb;Data Source=127.0.0.1;User Id=sama;Password=****;
and I set the connection string in the web.config file like this:
<connectionStrings>
<add name="MyDBContext"
connectionString="Data Source=127.0.0.1;User Id=sama;Password=****;Initial Catalog=sama;"
providerName="sqloledb" />
</connectionStrings>
but my website doesn't work!
I use Entity Framework code-first model in my project.
Where is my problem??
Thanks.
Try something like this...
<connectionStrings>
<add name="MyDBContext"
providerName="System.Data.SqlClient"
connectionString="Server=127.0.0.1;Database=sama;User Id=sama;Password=****;"/>
</connectionStrings>

NServiceBus SqlTransport - Where timeout manager should be run?

We are currently setting up NServiceBus with SqlTransport. Do we need use distributors in our test environment ? We have 2 clustered node are running the same application. Where timeout manager should be run in this scenario ?
The distributor is only used for MSMQ transport, see http://particular.net/articles/load-balancing-with-the-distributor section "When to use it?".
Updated:
Timeout manager runs on all endpoints connecting to the same sql database as the transport to avoid DTC.
Here is an example config:
<connectionStrings>
<add name="NServiceBus/Transport" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=nservicebus;Integrated Security=True" />
<add name="NServiceBus/Persistence" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=nservicebus;Integrated Security=True" />
</connectionStrings>
You need to reference NserviceBus.NHibernate, see http://particular.net/articles/relational-persistence-using-nhibernate---nservicebus-4.x for more info.

Connection String in a VB.NET Application

I used to work only with asp.net websites before. I know that in ASP.NET website, the connection string is found in the web.config file.
My problem is now I have started working with VB.NET applications which needed to be interfaced to a database. How is a connection string created and where should I put it?
Thanks!
Here's the whole app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbAsthmaConnectionString" connectionString="Data Source=9300-00\SQLEXPRESS;Initial Catalog=dbStore;Persist Security Info=True;User ID=johnsmith;Password=1234" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
</configuration>
Ok here is an axample :
1- Your app.config should look like this :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Amlakconn" connectionString ="Data Source=KHASHAYAR-PC\SQLEXPRESS;Initial Catalog=Amlak;Integrated Security=True"/>
</connectionStrings>
</configuration>
2- and in your code you can access the connectionsttring this way :
private string conn = ConfigurationManager.ConnectionStrings["Amlakconn"].ConnectionString;
go try It ;)
The other answers tell you where to put the connection string: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx
This is the easiest way to create a Connection String.
a) Create a Text file, rename the extension from TXT to UDL, press enter.
b) Double click the UDL file and choose OLEDB Provider For SQL Server > Next > type the Database Server name > Select the database and click Test Connection.
c) When the Test passes, close the UDL file and open it with notepad, the bold lines are the connection string:
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YourDatabase;Data Source=SQLEXPRESS
If you are working on a webapp project It's the same ! you can put that in the web.config file , and if you project is win app you can add an app.config file to your project and pu the connection string in there !
Non ASP.NET application can also use config file, it is just not named web.config. This file has exactly the same structure you know from ASP.NET including ConnectionStrings section. You can copy content from web.config and paste sections you need into app.config. In project it will show as app.config but in the bin folder it is named after the executable file name (with exe extension) plus ".config".
To create this file, go to project's Add -> New Item and select Application Configuration File. To get access to your connection strings create/copy <ConnectionString> section into <configuration> section and then use this code:
string conStr = ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conStr);
You need to add reference to `System.Configuration' assembly.