Entity Framework 4.1 修改每个开发人员单元测试环境的连接字符串
我们有一个 Entity Framework 4.1 设置、自动生成的 POCO 和用于与数据库交互的 DbContext 类。
自动生成的 DbContext 派生类如下所示:
public MyEntities()
: base("name=MyEntities")
{
}
app.config 中自动生成的连接字符串如下所示:
<connectionStrings>
<add name="MyEntities" connectionString="blah blah blah" providerName="System.Data.EntityClient" />
</connectionStrings>
一切都很好,并且开箱即用。
我们的每个开发人员都有自己的 SQL Developer 版本安装用于单元测试和开发目的,因此他们在进行单元测试时需要修改连接字符串以指向其安装。这些连接字符串位于单元测试项目的 App.config 文件中。
这目前是一个手动修改步骤,我曾想过以某种方式自动执行此操作。
现在,我可以在代码中计算出连接字符串,但是上面默认自动生成的“MyEntities”构造函数会阻止将其传递到实体框架构造函数中 - 它在任何构造函数上都没有连接字符串参数。 (尽管 DbContext 确实如此)。此外,我并不是特别想通过 DAL 代码深入连接字符串并深入到实体框架代码中 - 这在生产中不需要,因此修改我的代码只是为了单元测试的目的。
我原以为一定有某种方法可以覆盖 App.config 中的字符串。 这与使用本地 Appsettings 文件完成此操作类似。 (即
,但我似乎不知道如何对 ConfigurationManager.ConnectionStrings 条目执行此操作。
那么,人们在这里做什么呢?
We have an Entity framework 4.1 setup, auto generated POCOs and DbContext classes for interaction with the db.
The automatically generated DbContext derived class looks like this:
public MyEntities()
: base("name=MyEntities")
{
}
And the automatically generated connection string in app.config something like:
<connectionStrings>
<add name="MyEntities" connectionString="blah blah blah" providerName="System.Data.EntityClient" />
</connectionStrings>
That's all fine, and works right out of the box.
Each of our developers has their own SQL Developer edition install for unit testing and dev purposes, so they need to modify the connection string to point to their installation when doing unit testing. These connection strings are in the App.config file of the unit test project
This is currently a manual modification step, and I had thought to automate this in some way.
Now, I can go and work out the connection string in code, but passing this into the entity framework constructor is prevented by the default auto-generated "MyEntities" constructor above - it doesn't have a connection string parameter on any constructor. (Although DbContext does). Furthermore, I don't particularly want to plumb the connection string through the DAL code and down into the entity framework code - that isn't needed in production, so that's modifying my code just for the purposes of unit test.
I had thought there must be some way to override the string in App.config.
This would be similar to how this can be done with a local Appsettings file.(ie <appSettings file="Local.config"/>)
, but I can't seem to see how to do this for the ConfigurationManager.ConnectionStrings entries.
So, what does one do here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果命名约定不可接受,您可能需要在每台开发人员计算机上创建一个 SQL 别名以指向其自己的 SQL 实例。
本文:http:// /www.mssqltips.com/sqlservertip/1620/how-to-setup-and-use-a-sql-server-alias/ 提供创建别名的分步说明。
If naming convention is not acceptable, you might want to create a SQL Alias on each developper computer to point toward their own SQL Instance.
This article : http://www.mssqltips.com/sqlservertip/1620/how-to-setup-and-use-a-sql-server-alias/ provide step by step instructions to create an alias.
谢谢,这就是我最终所做的,灵感来自:
因为这仅适用于 DAL 单元测试,而不是生产,所以我根据用户环境变量动态修改连接字符串。
我永远不会建议在生产中这样做,您应该使用适当的转换等。
因此,对于单元测试,我在 app.configs 中定义了“data source=ENVVARTOSQLSERVER”。
然后在单元测试中使用 fn 来修改配置,保存并在启动时重新加载。
所以,就是这样。
开发人员需要设置一个环境变量,以便为其 SQL 提供适当的源路径。
Thanks, here is what I ended up doing, inspired by:
Because this is only for DAL unit tests, and not production, I modify the connection string on the fly, based on a users environment variable.
I would never advise this for production, you should be using appropriate transforms, etc.
So for unit test I have "data source=ENVVARTOSQLSERVER" defined in the app.configs.
Then a fn in the unit test to modify the config, save it, and reload it at startup.
So, that's it.
Devs need to set an environment variable so that the appropriate source path to their SQL is in place.