Use Azure Database (ASP.NET MVC) - c#

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

Related

attempt to open un named db failed. file with the same name exists, or it is located on UNC share

Actually i get a project from another pc and it has a database using local db having database with visual studio. before that i always used sql server for database. so i got stuck in it.
<add name="FZDatabase" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Talha\Desktop\Fashionzonefyp\Fashionzonefyp\App_DataFZDATABASE.MDF;Integrated Security=True" providerName="System.Data.SqlClient" />
here is the connection string. help as a totally new comer. any further suggestions and guidlines are also wellcomed... :)
Try this:
<add name="FZDatabase" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=C:\Users\Talha\Desktop\Fashionzonefyp\Fashionzonefyp\App_DataFZDATABASE.MDF;Integrated Security=True" providerName="System.Data.SqlClient" />
Actually there was a problem with my db name in connection string. and secondly my db was not in the directory of local db's which is by default (C:\Users\"Your pc name"\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\MSSQLLocalDB). thanks for help. :)

C# How to deploy/create setup for VS application with localdb?

Im trying to deploy application application with localdb. What I currently achieved is that I created mdf file from my localdb and add it to setup project (Visual Studio Installer). I modified connection of mdf file to localdb database. What I'm worried about this solution, when I'll edit database and install updates on client PC, if the data from mdf file will be lost. Is there a better way how to deploy or is this method valid?
Problem with this method is that I cant reach my stored procedure, even server explorer shows them.
Using VS 2015, SQL Server 2016 and EF 6 in my app.
Connection string, first one was initial one, connecting right to localdb, second one is connecting to mdf file. First one si working correctly, but only on dev machine, second one is having problems.
<connectionStrings>
<add name="MSDBContext" connectionString="data source=(localdb)\MSSQLLocalDB;initial catalog=MSDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
<add name="MSDBContextFile" connectionString="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\MSDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

I have two web.config files. Which one should I use?

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.

.mdf file missing under the App_Data folder in ASP.NET MVC

I was following the tutorial on creating the Code First Entity Framework model on Asp.NET MVC Application from this tutorial.
As I proceeded, a file with the .mdf extension should have been generated under the App_Data folder in my project (in the solution explorer section). Currently this folder is empty.
I have tried building the project and cleaning it. Moreover I have enabled Show All Files option in the folder. I even did a refresh. The Entity Framework works just fine and is able to connect and retrieve from the database. However the App_Data folder is empty.
Here is are my connection strings:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-UsingEntityFramework-20161002112829.mdf;Initial Catalog=aspnet-UsingEntityFramework-20161002112829;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=tcp:xxxxxxxxxx.net;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxxx;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Check your web.config and notice the connectionString and see the data source, maybe it is creating it under your SQL Server not inside the App_Data
The MDF File will be created when the database is created, and since this is a code first, the database will not be created unless you run the application and start connecting to the database, EF will create the database if it doesn't exist and will compare the schema to the schema in the Migrations folder and it will update the database if it is not up to date.
You can also open Nu Get Package Manager and run the command Update-Database to create and update the database.

host a database website with entity framework in the production server

I developed an asp.net website (using webforms) and for database connection I used Entity Framework. On my local computer everything is working fine. Now I try to host it I am having a problem. I have copied my files to the production server and attached my SQL Server database and set my connection string to:
<add name="ConnectionString"
connectionString="Data Source=SQL5003.Smarterasp.net;Initial Catalog=DB_9C4561_WFormBlog;User Id=DB_9C4561_WFormBlog_admin;Password=mydbpwd"
providerName="System.Data.EntityClient" />
but when I test my website am getting error:
Keyword not supported: 'data source'
When I removed "data source", it started complaining of "Initial catalog" keyword not supported.
Please help me out. You can refer me step by step tutorial or material that can put me through. Thanks in advance!
Try the below format, I have my database on smarterasp.net and it works well
<connectionStrings>
<clear />
<add name="ConnectionString" connectionString="Persist Security Info=False;database=DB_9C4561_WFormBlog;server=SQL5003.Smarterasp.net;user id=DB_9C4561_WFormBlog_admin;password=mydbpwd;Current Language=English;Connection Timeout=60;" />
</connectionStrings>

Categories