基于web.config动态设置LINQ dbml 文件中的连接字符串

1 篇文章 0 订阅

Ideally, you want your LINQ database connection strings defined outside your compiled code and to be held in the Web.Config instead.
 
Unfortunately, by default, they are added to your Settings.cs file which is then compiled away into your code. This has the effect of reducing your ability to easily configure your applications for different environments (i.e., you would need to have different compiled DLLs for dev, test and production).
 
It is also LESS secure because you can easily use reflection to examine your DLLs for passwords - whereas you can encrypt the web.config so it is only viewable to people who have permissions to the IIS console. This is more difficult to break.
 
The recommendations to remedy this issue in the following MSDN bloghttp://blogs.msdn.com/jongallant/archive/2007/11/25/linq-and-web-application-connection-strings.aspx is slightly wrong. He suggests that you remove the default constructor in the designer. This is bad because you would have to fix up the file every time you regenerate your dbml file. Instead, you should use what is provided to you and set the property on the designer for "Application Settings" to false and also do the following steps every time you will go for the drag and drop your stored procedure and user defined functions.

  1. Right click on any white space in dbml file.
  2. Click on properties option.
  3. Select drop down for Connection option in Properties window.
  4. Choose last option from drop down for Connection option which should be none.
  5. Close Properties window and press Save.

 

This allows you to define a default constructor in your own partial class that extends your dbml context designer classes like example:

 

In the above example, UserClassesDataContext is the class name of the LINQ to SQL(DBML) file and WebConnectionString is the name of the Connection String in Web.Config.
 
Happy coding.

原文:http://www.codeproject.com/Tips/189440/How-to-set-the-connection-string-in-your-LINQ-dbml.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值