Publishing asp.net mvc application Connection String - c#

I am using Entity Framework in my Application.
I am publishing application for first time to somee.com
But I have the problem with the connection string.
My connection string in web.config
<connectionStrings>
<add name="StoreDBContext" connectionString="metadata=res://*/Models.StoreModel.csdl|res://*/Models.StoreModel.ssdl|res://*/Models.StoreModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\StoreDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="StoreDB" connectionString="metadata=res://*/Models.StoreModel.csdl|res://*/Models.StoreModel.ssdl|res://*/Models.StoreModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\StoreDB.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
and by database connection string on somee database is :
workstation id=storebd.mssql.somee.com;packet size=4096;user id=****;pwd=****;data source=storebd.mssql.somee.com;persist security info=False;initial catalog=storebd
How should I format connection string in web.config file according to database connection provided by some.

Related

Connection strings in Entity Framework

I am referencing 2 databases in ASP.NET using Entity Framework.
In my web.config file, I can see the connection strings for the 2 databases:
<connectionStrings>
<add name="RContext"
connectionString="metadata=res://*/Models.RModel.csdl|res://*/Models.RModel.ssdl|res://*/Models.RModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="CEntities"
connectionString="metadata=res://*/Models.CModel.csdl|res://*/Models.CModel.ssdl|res://*/Models.CModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQLEXPRESS;initial catalog=RStreamline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
Can I somehow implement alternate connection strings where the datasource refers to the prod server for the release?
This is typically handled with web.config transforms.
In your project you would have:
web.config
web.Release.config
For example in your web.Release.config transform you would have something like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="https://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="RContext"
connectionString="RContext-Prod-Connection-String"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
<add name="CEntities"
connectionString="CEntities-Prod-Connection-String"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
You'll notice the xdt:Transform="SetAttributes" xdt:Locator="Match(name)" bit, which says, in the main web.config find the connectionString by name and replace its attributes with the ones defined here.
This will automatically happen when you publish the application.

how to solve 'Keyword not supported: 'metadata'

I'm trying to connect to my database with SQL client but I can't connect.
Here is my connection string:
<add name="RegisterDBEntities"
connectionString="metadata=res://*/Register.csdl|res://*/Register.ssdl|res://*/Register.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\RegisterDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />

Restless connection to the Entity Framework

I have an ASP.net web application hosted in IIS. This application use SQL Server.
I removed connection string in web.config. But when I start the application, it is still connected to it.
The connection string was nowhere explicitly stated.
how does it work?
By default the machine.config located in C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config have the following connection string configured.
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
By leaving the <connectionStrings> section of your web.config empty you are not removing the default connectionstring. To ensure that you have no configured connection string you should use a <clear /> element in your web.config file.
<connectionStrings>
<clear />
</connectionStrings>

Cannot connect to local DB at another PC. ASP .NET MVC4

I have created an mvc 4 database application on "PC1". I just have chosen by right click "Database SQL Server" and created Entity Framework with Database First approach. It works okay at "PC1". But when I copied my project to another PC, then the project failed.
I do not have an SQL Server at the PC. I just copied the mvc 4 project with local database.
The error is: "...(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
I suppose that I should rewrite connection string. I've done this steps:
http://msdn.microsoft.com/en-us/library/hh873188.aspx
And I have tried to change a connection string from:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
To:
I have added name of PC(TomPC) to connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.TomPC\SQLEXPRESS;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.TomPC\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.TomPC\SQLEXPRESS;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
However, it fails with the same error.
I would greatly appreciate any help!
You will also need to ensure that TCP/IP is enabled in SQL Server config on TomPC, and also open up port 1433 (the default port, but it might be different, e.g. if there's more than one instance of SQL running on that PC).
Data Source=.\SQLEXPRESS
The . means "localhost" or 127.0.0.1
So adding the . means localhost\SQLEXPRESS or 127.0.0.1\SQLEXPRESS
You need to change it to:
Data Source=TomPC\SQLEXPRESS
I've found an answer. The application could not connect as connection string should be modified as LocalDB:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=aspnet-TestApplication-20140918110411;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="CustomerDatabaseEntities" connectionString="metadata=res://*/UserModel.csdl|res://*/UserModel.ssdl|res://*/UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\CustomerDatabase.mdf;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
http://www.asp.net/mvc/tutorials/mvc-5/introduction/creating-a-connection-string

How to configure connection string to RDS Instance (MSSQLServer Express)

I have connected from my PC to my instance RDS (MSSQL Server Express) using Managment Studio without problems, because I have already configured the policy groups and other stuff.
My "simple" problem is when I try to connect from my WebApp, I have a web.config and this is my configuration in ASP.NET MVC 5 Project... very easy:
Can you help to configure my connection string correctly?
Thanks!!
<add name="Entities" connectionString="metadata=res://*/Business.Interactive.Model.csdl|res://*/Business.Interactive.Model.ssdl|res://*/Business.Interactive.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com:1433;Database=Business.interactive.gci;User Id=userroot;
Password=XXXXXXXX;pooling=False;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="myBusinessContext" connectionString="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com:1433;Database=Business.interactive.gci;User Id=userroot;
Password=XXXXXXXX;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Ok, The problem was the length of the name of the database and I also I had to delete the number port:
Finally:
<add name="Entities" connectionString="metadata=res://*/Business.Interactive.Model.csdl|res://*/Business.Interactive.Model.ssdl|res://*/Business.Interactive.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com;Database=Businessgci;User Id=userroot;
Password=XXXXXXXX;pooling=False;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="myBusinessContext" connectionString="Data Source=aa1XXXXXXXXXbi.cruXXXXXXym4.eu-west-1.rds.amazonaws.com;Database=Businessgci;User Id=userroot;
Password=XXXXXXXX;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
Regards!!

Categories