将元素的 connectionStringName 设置为 LocalSqlServer 的目的?
1) 默认情况下,配置元素的 connectionStringName 属性设置为 LocalSqlServer,据我所知,该属性引用 < machine.config 文件中的 /em> 元素。
a) 我假设此连接字符串引用数据库aspnetdb.mdf?!
b) 我知道 aspnetdb.mdf 用于我们不手动创建成员资格或配置文件数据库(通过调用 aspnet_regsql )的情况,但我仍然不明白将 connectionStringName 属性设置为 LocalSqlServer 的配置元素的目的是什么? 即,他们何时以及为何需要访问该数据库?
c) 如果我们通过 aspnet_regsql 手动设置成员数据库,而不使用 aspnetdb.mdf,会发生什么? 配置元素如何知道我们没有使用 aspnetdb.mdf ,从而尝试访问我们创建的数据库?
2) 如果我们希望 machine.config 中的 LocalSqlServer 条目指向其他数据库文件,我们可以执行以下操作:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name=”LocalSqlServer” ... />
</connectionStrings>
我了解
元素是取消任何先前声明的同名元素,但在上面的示例中,我们只是更改了现有连接的属性,因此 machine.config 不会没有两个同名的连接,那么为什么我们必须包含
元素?
谢谢
1) By default configuration elements have their connectionStringName attribute set to LocalSqlServer, and as far as I know, this attribute refers to connection defined in the element in machine.config file.
a) I assume this connection string refers to database aspnetdb.mdf?!
b) I understand aspnetdb.mdf is used in cases where we don’t manually create membership or profile database ( by calling aspnet_regsql ), but I still don’t understand the purpose of configuration elements having connectionStringName attribute set to LocalSqlServer set? Namely, when and why would they need to access this database?
c) What happens if we manually set membership database via aspnet_regsql and thus don’t use aspnetdb.mdf? How will configuration elements know that we’re not using aspnetdb.mdf and thus instead try to access database we created?
2) If we wanted LocalSqlServer entry from machine.config to point to some other database file, we could do the following:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name=”LocalSqlServer” ... />
</connectionStrings>
I understand that the purpose of <remove>
element is to cancel any previously declared elements with same name, but in above example we simply changed the attribute of already existing connection, and as such machine.config doesn’t have two connections with same name, so why did we have to include <remove>
element?
thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 MSDN 上的 connectionStrings 元素文章:
文件的该部分未修改,它会自动具有该连接字符串。
From the connectionStrings element article on MSDN:
So if that section of the config file is not modified, it automatically has that connection string.
使用的约定是可以选择完成相同任务的众多约定之一,但当时 MS ASP.NET 团队中的人员实际上是唯一能够说出“为什么”这组约定的人用过的。 我的理解是,目前配置的目的是为了让初学者尽可能容易上手。 即运行一些向导,自动生成具有预配置设置的数据库,拖放一些安全控件,然后它们就可以使用了。 由于这是为初学者设计的,经验丰富的开发人员会遇到与您现在遇到的相同的问题,因为深入了解各个部分如何组合在一起并不简单。
您在 machine.config 中会注意到的一件事是,所有提供程序(成员资格、角色、配置文件等)都使用此 LocalSqlServer 连接字符串名称,这再次支持初学者场景。 因此,要使用您自己的数据库,您需要删除 LocalSqlServer 的默认定义并定义您自己的。 配置文件定义中没有替换元素,因此您必须使用删除/添加序列,这是逻辑上的等效项。 通过更改连接字符串并将其名称保留为 LocalSqlServer,machine.config 中的所有提供程序都会指向您的数据库。 这为您提供了数据库的默认提供程序定义。
现在,如果您想自定义提供程序定义,您可以将它们添加到您自己的 Web.config 中并更改其设置。 此时,您可以将 LocalSqlServer 保留为自定义提供程序定义的连接字符串,或者您可以创建自己的连接字符串,然后将自定义提供程序定义指向您自己的连接字符串,这样您就不再需要担心 LocalSqlServer 了。 如果从 web.config 中删除 LocalSqlServer,则需要将自定义提供程序定义添加到引用数据库字符串的自己的 web.config 中。
希望这有帮助,
乔
The convention used is one of many that could have been chosen to accomplish the same task, but the people who were on the ASP.NET team at MS at the time are really the only ones who can say "why" that set of conventions was used. My understanding is that the purpose of the current configuration is to make it as easy as possible for a beginner to get started. i.e. run some wizards, automatically generate database with preconfigured settings, drag-n-drop a few security controls and they have something to work with. Since this was designed for beginners, more experienced developers run into the same set of questions that you're having now because digging into how the pieces fit together isn't simple.
One of the things you'll notice in machine.config is that all of the providers (Membership, Roles, Profile, etc) use this LocalSqlServer connection string name, which again supports the beginner scenario. Therefore, to use youur own database, you need to remove the default definition of LocalSqlServer and define your own. There isn't a replace element in the config file definition, so you have to use the remove/add sequence, which is the logical equivalent. By changing the connection string and leaving its name as LocalSqlServer, all of the providers in machine.config get pointed at your DB. This gives you the default provider definitions in for your database.
Now, if you wanted to customize the provider definitions, you could add them to your own Web.config and change their settings. At that point, you could leave LocalSqlServer as the connection string for the custom provider definitions or you could create your own connnection string and then point your custom provider definitions at your own connection string and you won't need to worry about LocalSqlServer anymore. If you remove LocalSqlServer from you web.config, you'll need to add custom provider definitions to your own web.config that reference your database string.
Hope this helps,
Joe