Problem publishing ASP.NET application with a SQL Server database

0

I've created an ASP.NET website which runs perfectly from Visual Studio. It connects to a SQL Server .mdf database file in the App_Data folder. I published the project using FTP. The live website cannot however connect to the database, as it is LocalDB, and my host uses a SQL Server (SqlException (0x80131904))

I uploaded an identical copy of the local .mdf database file from my computer to the host's SQL Server. I then tried to form a data connection with the server database from Visual Studio, however my host doesn't allow remote database access, for security reasons, resulting in error 40.

I'm using ASP.NET MVC in my application so I need access to the database to create models. My host has said "you can access SQL Server databases using ASP scripts running on your presence".

So my question is: how do I create models based on my host's SQL Server database when remote access isn't allowed?

asp.net
sql-server
model-view-controller
asked on Stack Overflow Jun 11, 2019 by 0liveradam8 • edited Jun 11, 2019 by marc_s

1 Answer

1

When you're testing with Visual Studio, you should connect to the local copy of your database. When you're ready to publish your application, copy the database to the remote sql server and attach it. The copy of your application which is published should then connect to the uploaded database on that server.

That way you have one environment for testing and one for real use. There shouldn't be any need for Visual Studio to connect to the remote database.

P.S. To allow you to connect to different databases in different environments, usually you set your database connection string in the web.config file. So if you create different config transforms for each build configuration (e.g. debug and release) then visual studio will create the correct version of the config file when you do a Publish operation. See https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations for info about configuration transforms.

answered on Stack Overflow Jun 11, 2019 by ADyson

User contributions licensed under CC BY-SA 3.0