我可以阻止 dbml 设计器向 dbml 文件添加连接字符串吗?
我们有一个自定义函数AppSettings.GetConnectionString()
,它总是被调用来确定应该使用的连接字符串。这个函数如何工作对于讨论来说并不重要。可以说它返回一个连接字符串,我必须使用它。
我希望我的 LINQ to SQL DataContext 使用它,因此我从 dbml 文件中删除了所有连接字符串信息,并使用默认构造函数创建了一个分部类,如下所示:
public partial class SampleDataContext
{
public SampleDataContext() : base(AppSettings.GetConnectionString()) { }
}
在我使用设计器将表拖放到图表中之前,这种方法工作正常。将表拖到图表中的操作会执行一些不需要的操作:
- 将创建一个设置文件
- 将创建一个 app.config 文件
- 我的 dbml 文件将嵌入连接字符串
所有这些都在我保存之前完成文件!
当我保存图表时,将重新创建设计器文件,并且它将包含其自己的默认构造函数,该构造函数使用错误的连接字符串。当然,这意味着我的 DataContext 现在有两个默认构造函数,我无法再构建!
我可以撤销所有这些不好的事情,但这很烦人。每次更改后我都必须手动删除连接字符串和新文件!
我是否可以在不询问的情况下阻止设计师进行这些更改?
编辑
使用 AppSettings.GetConnectionString() 方法的要求是在游戏后期强加给我的。我曾经使用的东西与它为我生成的东西非常相似。有相当多的地方调用了默认构造函数。我知道我应该更改它们以另一种方式创建数据上下文(使用不同的构造函数、静态方法、工厂等)。这种改变只会有点烦人,因为它只需要做一次。但我觉得,这回避了真正的问题。 dbml 文件和配置文件仍将包含不正确的(如果未使用)连接字符串,这充其量可能会让其他开发人员感到困惑。
We have a custom function AppSettings.GetConnectionString()
which is always called to determine the connection string that should be used. How this function works is unimportant to the discussion. It suffices to say that it returns a connection string and I have to use it.
I want my LINQ to SQL DataContext to use this so I removed all connection string informatin from the dbml file and created a partial class with a default constructor like this:
public partial class SampleDataContext
{
public SampleDataContext() : base(AppSettings.GetConnectionString()) { }
}
This works fine until I use the designer to drag and drop a table into the diagram. The act of dragging a table into the diagram will do several unwanted things:
- A settings file will be created
- A app.config file will be created
- My dbml file will have the connection string embedded in it
All of this is done before I even save the file!
When I save the diagram the designer file is recreated and it will contain its own default constructor which uses the wrong connection string. Of course this means my DataContext now has two default constructors and I can't build anymore!
I can undo all of these bad things but it is annoying. I have to manually remove the connection string and the new files after each change!
Is there anyway I can stop the designer from making these changes without asking?
EDIT
The requirement to use the AppSettings.GetConnectionString()
method was imposed on me rather late in the game. I used to use something very similar to what it generates for me. There are quite a few places that call the default constructor. I am aware that I dould change them all to create the data context in another way (using a different constructor, static method, factory, ect..). That kind of change would only be slightly annoying since it would only have to be done once. However, I feel, that it is sidestepping the real issue. The dbml file and configuration files would still contain an incorrect, if unused, connection string which at best could confuse other developers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 DBML 设计器中,您可以右键单击任何空白区域,然后单击“属性”(与右键单击 DBML 文件并单击“属性”不同)。
从那里展开“连接”选项。将“应用程序设置”设置为 False 并清除“连接字符串”设置。设计者使用这些设置来创建默认构造函数。
从那里,您可以使用在 Designer.cs 文件外部创建的默认构造函数。不幸的是,每次向设计器添加任何新表时,您都必须重复此过程。这很烦人,我感受到你的痛苦。
While in the designer for the DBML, you can right-click on any white-space, and click on Properties (not the same as right-clicking on the DBML file and clicking Properties).
From there, expand the "Connection" option. Set "Application Settings" to False and clear out the "Connection String" setting. These settings are what the designer uses to create that default constructor.
From there, you can use the default constructor you've created outside of the designer.cs file. Unfortunately, you'll have to repeat this process everytime you add any new tables to the designer. It's annoying, and I feel your pain.
您可以使用类似 SqlMetal 的内容来生成您自己的 DataContext 设计器文件,但你是对的 - 默认情况下 DataContext 很难破解。
您的另一个选择是从工厂方法获取 DataContext,以便您可以隐藏实际使用的构造函数。如果您通过像 Castle Windsor 这样的 IoC 框架来做到这一点就更好了。然后你就可以做类似的事情:
You could use something like SqlMetal to generate your own DataContext designer file, but you're right - the DataContext by default is pretty hard to crack open.
Your other option is to obtain your DataContext from a factory method, so that you can hide which constructor is actually used. Even better if you do this via an IoC framework like Castle Windsor. Then you'd be able to do things like:
这有点“hacky”,但您可以向构造函数添加一个参数(未使用的参数?),在任何地方使用此构造函数,设计者创建的默认构造函数不会给您带来任何问题。
This is a bit 'hacky' but you could add a parameter to your constructor (an unused one?), use this constructor everywhere and the default one created by the designer won't cause you any problems.