I recently built an application using visual studio express. This application was built using a local DB provided by VS express. I now have a subscription (free trial) to Microsoft Azure. What changes do I need to make to my project in order to point my project to the newly created Azure DB?
I also noticed I have 2 web.config files. one in the root directory and one in the views folder.
I believe I have to make an update to my connection string, but in a specific config file or both?
Here is my root directory web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source= LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20160922101748.mdf;Initial Catalog=aspnet-MvcMovie-20160922101748;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
and my (views) web.config connection string:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
Any help would be highly appreciated.
I believe only one web.config with connection strings should be enough. Keep the one in the root.
Download Azure DB connection strings from the Azure portal and replace connectionString values in the web.config file with the Azure DB connection string(s).
You might also need to whitelist your machine's IP address in the Azure Portal to allow connections to Azure DB.
You only need to change the web.config on the root folder as thats the one used by the application.
And you are right, you need to point the connection string to your Azure database.
You definitely only need to update the one web.config in the root of the application with the connection string. The other web.config in the views folder is needed for your views to use html helpers and to make routing work as you expect in an mvc application so do not remove it.
Related
I'm trying to publish a website with a locally stored database on my localhost. The problem is that I can't enter into any page who have a database embedded as a data source. All databases are kept into the App_Data folder, and I've used both into attachdbfilename:
1. |DataDirectory|/Database1.mdf
2. C:/Users/username/Desktop/websitefolder/App_Data/Database1.mdf
I've tried the other topics to solve my problem, but they are not mentioning that's a published website. The site works without any problem on localhost with the port assigned, but any page with a database/source related shows this error.
There is my config file:
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf; User Instance=false;Integrated Security=false" providerName="System.Data.SqlClient" />
<add name="connect" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database2.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
Try adding the "InitialCatalog=DatabaseName;" to your connection string:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"
Here are some examples from the MSDN website to refer to.
I have ASP.NET MVC project.
In WebConfig file I have a connection to local db.
Here is code:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=DESKTOP-SV2O11U;Initial Catalog=RIS_Main;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="RIS_MainEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-SV2O11U;initial catalog=RIS_Main;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
But the problem in that I working on two pcs, work and home.
So it has a different name.And I need to change DataSource in DefaultConnection
Is there any variant to keep 2 pcs name in connectionStrings?
There are two ways to solve it:
If your database server is on the same host as your application server, you can access the database via localhost host.
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=RIS_Main;Integrated Security=True" providerName="System.Data.SqlClient" />
The other way is to have multiple transformations of the same config file (like you already have with Debug and Release).
Using this way, depending on your build configuration you can have multiple completely different connection strings. Visual Studio will decide during build which one to acutually use.
Image Source
I'm new in ASP.NET Development
I create MVC Project and as I understood it creates with default .mdf database.
I working with it and all was great, but I think about deploying app to azure.
So I need to use azure database.
I create it , all ok.
I read article how connect to azure db.
As I understood , I need connection string
Here it is
Server=tcp:smartsolutionsserver.database.windows.net,1433;Initial Catalog=smartdatabase;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
As I understood I need to paste it to Web.Release.config
I do this, so now it have look like this
<connectionStrings>
<add name="DefaultConnection"
connectionString="Server=tcp:smartsolutionsserver.database.windows.net,1433;Initial Catalog=smartdatabase;Persist Security Info=False;User ID=*********;Password=******;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
But as I understood .mdf database connections wrote in Web.config file
And looks like this
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-SmartSolutions-20170124034333.mdf;Initial Catalog=aspnet-SmartSolutions-20170124034333;Integrated Security=True" providerName="System.Data.SqlClient" />
Tell me, how I can connect to azure database and use it instead of local .mdf database?
Thank's so much for help.
UPDATE
I try to add 2 new tables and publish app. After connecting to database from SQL Server Object Explorer, I see that tables added.
But when I try to register new user I see this
On local all ok
Screen
So my error was in The model backing the 'ApplicationDbContext' context has changed since the database was created
I go to table __MigrationHistory and delete row. After that, all okay. All works
# juunas Thank's for advice. I do this and see error. Also it can visible if you use IIS Console
How to change database that web configuration is created?
When I use Asp.net Configuration, visual studio create a default data base (ASPNETDB.MDF),
I want use my database for my web site .
How to do this?
Just add this tab in <connectionStrings> of your Web.Config
<connectionStrings>
<add name="YourConnectionStringName"
connectionString="DataSource=yourDataSource;Initial Catalog=YourDataBaseName;User ID=YourID;Password=YourPassword"
providerName="System.Data.SqlClient" />
</connectionStrings>
You should a add database using this line in web.config
replace with aspnetdb connection string
<connectionStrings>
<add name="LoanCalculatorConnectionString" connectionString="Data Source=servername;Initial Catalog=dbname;Persist Security Info=True;User ID=888;Password=8888"
providerName="System.Data.SqlClient" />
</connectionStrings>
To this porpus use this way:
C://windows/Microsoft.net/Framework/v2.0.50727/aspnet_regsql.exe
. is run
Next--> Configuration SQL server for application service --> select server and database --> Next --> Finish
I am trying to configure a different location for my local db.
I read an article on configuring connection strings and it suggests I can replace |DataDirectory| in the web config with ~/MyDirectory where ~ is the web app root. So I have input the connection string as:
<connectionStrings>
<add name="Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Context-20130829161219;Integrated Security=SSPI;AttachDBFilename=~/MyDirectory/Context-20130829161219.mdf"
providerName="System.Data.SqlClient" />
Unfortunately, when I run the app it doesn't seem to create the db at all. I can imagine it's only something very small that I am doing wrong but I would appreciate if someone can point me in the right direction.
Many Thanks
Lets try with below string it will help you
<add name="Context" connectionString="metadata=res://*/Models.Datacontext.csdl|res://*/Models.Datacontext.ssdl|res://*/Models.Datacontext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\SQLInstance;initial catalog=Context-20130829161219;persist security info=True;user id=sa;password=Yourpassword;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
or replace your Source=(LocalDb)\SQLInstance;initial with Source=.\SQLInstance;initial
Try running the Update-Database cmdlet from the package manager console - you should at least see some output indicating why it doesn't work.