简体   繁体   English

使用SQL Server数据库发布ASP.NET应用程序时出现问题

[英]Problem publishing ASP.NET application with a SQL Server database

I've created an ASP.NET website which runs perfectly from Visual Studio. 我已经创建了一个可以从Visual Studio完美运行的ASP.NET网站。 It connects to a SQL Server .mdf database file in the App_Data folder. 它连接到App_Data文件夹中的SQL Server .mdf数据库文件。 I published the project using FTP. 我使用FTP发布了该项目。 The live website cannot however connect to the database, as it is LocalDB , and my host uses a SQL Server (SqlException (0x80131904)) 但是,实时网站无法连接到数据库,因为它是LocalDB ,并且我的主机使用SQL Server(SqlException(0x80131904))

I uploaded an identical copy of the local .mdf database file from my computer to the host's SQL Server. 我从计算机.mdf本地.mdf数据库文件的相同副本上载到主机的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. 然后,我尝试从Visual Studio与服务器数据库建立数据连接,但是出于安全原因,我的主机不允许远程数据库访问,导致错误40。

I'm using ASP.NET MVC in my application so I need access to the database to create models. 我在应用程序中使用ASP.NET MVC,因此需要访问数据库才能创建模型。 My host has said "you can access SQL Server databases using ASP scripts running on your presence" . 我的房东说: “您可以使用存在时运行的ASP脚本访问SQL Server数据库”

So my question is: how do I create models based on my host's SQL Server database when remote access isn't allowed? 所以我的问题是:当不允许远程访问时,如何基于主机的SQL Server数据库创建模型?

When you're testing with Visual Studio, you should connect to the local copy of your database. 使用Visual Studio进行测试时,应连接到数据库的本地副本。 When you're ready to publish your application, copy the database to the remote sql server and attach it. 准备发布应用程序时,请将数据库复制到远程sql服务器并附加它。 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. Visual Studio不需要连接到远程数据库。

PS To allow you to connect to different databases in different environments, usually you set your database connection string in the web.config file. PS为了允许您连接到不同环境中的不同数据库,通常需要在web.config文件中设置数据库连接字符串。 So if you create different config transforms for each build configuration (eg debug and release) then visual studio will create the correct version of the config file when you do a Publish operation. 因此,如果您为每个构建配置(例如调试和发行版)创建不同的配置转换,则Visual Studio将在执行发布操作时创建正确版本的配置文件。 See https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations for info about configuration transforms. 有关配置转换的信息,请参见https://docs.microsoft.com/zh-cn/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM