简体   繁体   English

使用LINQ“干净”地部署ASP.NET应用程序到SQL Server

[英]“Cleanly” Deploying an ASP.NET Application with LINQ to SQL Server

In my development environment, my SQL Server is PHILIP\\SQLEXPRESS . 在开发环境中,我的SQL Server是PHILIP\\SQLEXPRESS In testing, it's ANNIE , and the live environment will have a third name yet to be determined. 在测试中,它是ANNIE ,实时环境将有一个尚待确定的名字。 I would have assumed that updating the following statement in web.config would have been enough: 我本以为只要在web.config中更新以下语句就足够了:

<add name="MyConnectionString"providerName="System.Data.SqlClient" 
connectionString="Data Source=PHILIP\SQLEXPRESS;Initial Catalog=MyDtabase;Integrated Security=True" />

When using SqlConnection , SqlCommand , SqlDataReader and friends, that's all it took. 使用SqlConnectionSqlCommandSqlDataReader和朋友时,仅此而已。 Using LINQ, it doesn't seem to work that nicely. 使用LINQ,它似乎不能很好地工作。 I see the servername repeated in my .dbml file as well as in Settings.settings . 我在.dbml文件以及Settings.settings看到服务器名称重复。 After changing it in all of those places, I get it to work. 在所有这些地方进行更改后,我就可以使用它。 However if I'm doing a few deployments per day during testing, I want to avoid this regimen. 但是,如果我每天在测试过程中进行几次部署,则要避免这种方案。

My question is: is there a programmatic solution for LINQ to SQL that will allow me to specify the connection string once, preferably in web.config , and get everybody else to refer to it? 我的问题是:是否有针对LINQ to SQL的程序化解决方案,允许我一次指定连接字符串,最好是在web.config ,并让其他人引用它?

-- EDIT -- -编辑-

The following connection statement is in my .dbml file: 以下连接语句在我的.dbml文件中:

<Connection Mode="AppSettings" ConnectionString="Data Source=PHILIP\SQLEXPRESS;
InitialCatalog=MyDatabase;Integrated Security=True" 
SettingsObjectName="MyDatabase_Data_Access_Layer.Properties.Settings" 
SettingsPropertyName="MyConnectionString" Provider="System.Data.SqlClient" />

On your Linq-to-SQL model (the *.dbml file), you should be able to bring up its properties. 在Linq-to-SQL模型(* .dbml文件)上,您应该能够显示其属性。 In the property grid, there is a Connection setting which allows you to specify where and how to store that connection information for Linq-to-SQL. 在属性网格中,有一个“ Connection设置,该设置使您可以指定在哪里以及如何存储Linq-to-SQL的连接信息。 This typically refers to your app.config/web.config. 这通常是指您的app.config / web.config。 See my sample here: 在这里查看我的示例:

替代文字

(full size image at http://i43.tinypic.com/xazbrb.png ) (全尺寸图片位于http://i43.tinypic.com/xazbrb.png

There's nothing stopping you from using the very same connection string as your "normal" app uses - actually, that's quite a good idea, in fact! 没有什么可以阻止您使用与“常规”应用程序相同的连接字符串的-实际上,这确实是一个好主意!

So you should be able to basically change your SQL Server connection string in one place (in your app.config / web.config) and that should be all you need to do! 因此,您应该基本上可以在一个位置 (在app.config / web.config中)更改SQL Server连接字符串, 就是您所需要做的! Doesn't get much easier than that, does it? 没有比这更容易的了吗?

is there a programmatic solution for LINQ to SQL that will allow me to specify the connection string once, preferably in web.config, and get everybody else to refer to it? 是否有针对LINQ to SQL的编程解决方案,该解决方案使我可以一次指定连接字符串(最好在web.config中指定),并让其他人引用它?

That's what I do with LINQ-to-SQL, and it works. 这就是我使用LINQ-to-SQL所做的事情,并且有效。

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

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