[ASP.NET]-Connection String Manipulation in ApplicationDbContext - asp.net

I had a challenge today at work (Solved already but I'll like to put it out here maybe there is a better and smarter way of doing it)
I am working on VSTS with git on a project with another developer.
We use different connection strings so what we do is when I pull, I comment his connection string and activate mine.
When he pulls, he comments mine and activates his.
We don't want to create another git-branch.
What is the best way to achieve this?

Here is an instance of how I did it.
Editing my Web.Config file
Note: the names: DefaultConnection, LocalConnection, livedbsource, localdbsource, localMachineName
<connectionStrings>
<!--Live-->
<add name="DefaultConnection" connectionString="Data Source=livedbsource;Initial Catalog=livedbname;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
<!--Dev-->
<add name="LocalConnection" connectionString="Data Source=localdbsource;Initial Catalog=localdbname;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
</connectionStrings>
And for the ApplicationDbContext
static string ServerName = System.Net.Dns.GetHostName();
//setting what connection string to use - tenary operator
static string ConnectionStringID = (ServerName == "localMachineName") ? "LocalConnection" : "DefaultConnection";
public ApplicationDbContext()
: base(ConnectionStringID, throwIfV1Schema: false)
{
}
On Windows 7 and higher, You can get the localMachineName pressing the Windows Logo and searching for System Information, then look for 'System Name'
Here is the YouTube video I did it in Visual Studio 2017 on a live project and it worked.
https://youtu.be/oKg6ewKhkYs

Related

Entity Framework connection string issue

I am receiving the following error which I believe is a problem with my connection string on GoDaddy. I've copied from an example on Stack Overflow, but it doesn't seem to work. The first part of the stack trace that I'm getting is:
[MetadataException: Unable to load the specified metadata resource.]
System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String
assemblyName, String resourceName, ICollection`1 uriRegistry,
MetadataArtifactAssemblyResolver resolver) +2184231
I have a solution with only one project and, of course, the edmx is in that project.
Here is my connection string:
<add name="FCGUIDE_Entities"
connectionString="
metadata=res://*/CoreModel.csdl|res://*/CoreModel.ssdl|res://*/CoreModel.msl;
provider=System.Data.SqlClient;
provider connection string='
Data Source=susanbfarrar.db.9319451.hostedresource.com;
Initial Catalog=susanbfarrar;
integrated security=False;
multipleactiveresultsets=True;
App=EntityFramework;
User ID=*****;
Password=*****
Database=*****;'"
providerName="System.Data.EntityClient" />
Could you replace ' with " and try it again?
<add name="FCGUIDE_Entities"
connectionString="
metadata=res://*/CoreModel.csdl|res://*/CoreModel.ssdl|res://*/CoreModel.msl;
provider=System.Data.SqlClient;
provider connection string="
Data Source=susanbfarrar.db.9319451.hostedresource.com;
Initial Catalog=susanbfarrar;
integrated security=False;
multipleactiveresultsets=True;
App=EntityFramework;
User ID=*****;
Password=*****
Database=*****;""
providerName="System.Data.EntityClient" />
You already have Initial Catalog, so remove Database
Password=*****Database=*****;"
Like this -
Password=*****;"

Have connection strings as well as common config source in asp.net or wcf websites

Hi I am looking for a solution where I have some common connection strings which I have
a common connections strings file but also need some specific add and remove connection strings in some sites...but do not see a way to do it...
<configuration>
<connectionStrings configSource="connections.config">
<remove name="AppConnectionString" />
<add name="AppConnectionString" connectionString="data source=(local);initial catalog=xyz;integrated security=True;MultipleActiveResultSets=True;" />
<connectionStrings/>
</configuration>
With file connections.config containing
<connectionStrings>
<add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
<add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
Thanks,
Bala
If this does not work as you have expressed it above (which is a shame in my opinon), you have a number of other options:
Include the common connection strings in the connection string config, and then use the application settings config for the app-specific connection strings.
As an alternative, use a custom configuration file for the app-specific connection strings and load it in for each app.
When you build your apps inject the correct connection string keys into the config file using something like powershell. This script can run as a post build task in your build script.
Otherwise, when you package or stage your release, ask the release team to run the script as a manual step (or add it into your deployment script).

How to make relative path to database in app.config using SQLite/entity framework

I want to be able to use a relative path to use a SQLite DB on more than 1 pc.
the connectionstring in the app.config looks like this now:
<add name="DBPersonEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite;provider connection string='data source="C:\Users\Dreeze\Documents\Test DB2\DBPerson.s3db"'" providerName="System.Data.EntityClient" />
The DB file is in the same folder as the app... i would like to make the path relative so it refers to the apps folder. Can anyone help me change this connectionstring?
Use this connectionString
<add name="DWContext" connectionString="Data Source=|DataDirectory|DBPerson.s3db" providerName="System.Data.SQLite" />
Then set DataDirectory path on your code before initializing Context objext.
string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", path);
You should be able to use the data directory substitution string:
provider connection string='data source="|DataDirectory|DBPerson.s3db"'

Pre-Generating views to improve query performance in Entity Framework

I am trying to pre-generate view metadata for my Entity Framework project and have run across a problem on the last step when using this resource:
http://msdn.microsoft.com/en-us/library/bb896240.aspx
Everything else is compiling great, but I'm getting an error when I run my application, and I suspect it's due to a problem with the final step, 're-adding mapping and model files as embedded resources for ASP.NET projects.'
I'm receiving 'Unable to load the specified metadata resource.' and my connection string is as follows:
<add name="myEntities"
connectionString="metadata=
.\DataStructure.csdl|
.\DataStructure.ssdl|
.\DataStructure.msl;provider=System.Data.SqlClient;provider
connection string="Data Source=x;Initial Catalog=x;Persist Security Info=True;User ID=x;Password=x;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
It is suggested in the document that my connection string file should include the following but haven't been able to get it right in any configuration:
Metadata=res://<assemblyFullName>/<resourceName>;
Metadata=res://*/<resourceName>;
Metadata=res://*;
Assuming my assembly name is DataStructure.EF, how should my string be constructed?
http://msdn.microsoft.com/en-us/library/cc716756.aspx
<connectionStrings>
<add name="AdventureWorksEntities"
connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />
</connectionStrings>
I compiled a full guide for anybody else having problems and published it here: http://kewney.com/posts/software-development/pre-generating-views-to-improve-query-performance-in-aspnet-mvc-3-entity-framework

ASP.NET Attempt to attach auto-named a database failed?

Parser Error Message: An attempt to
attach an auto-named database for file
D:\Projects\Damnation\Damnation.Website\Damnation.Website.Tracker\BugNET_WAP\App_Data\aspnetdb.mdf
failed. A database with the same name
exists, or specified file cannot be
opened, or it is located on UNC share.
Why could this be happening? My connection string doesn't use |DataDirectory|, it is:
<connectionStrings>
<add name="BugNET" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dnmain;Integrated Security=True;"/>
</connectionStrings>
I get this error when I try to load default.aspx, after installing BugNET.
Apparently, and certainly for some obscure reason. There is a mistery connection string coming from... nowhere.
Problem solved adding a <clear/> tag.
<connectionStrings>
<clear/>
<add name="BugNET" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=dnmain;Integrated Security=True;"/>
</connectionStrings>
file
D:\Projects\Damnation\Damnation.Website\Damnation.Website.Tracker\BugNET_WAP\App_Data**aspnetdb.mdf**
failed.
This is the ASP.NET standard membership system here - not your "regular" database. There must be a second connection string somewhere that references this aspnetdb.mdf and tries to attach it from a file.
Search your solution for this file name - it must be somewhere!

Resources