首先在EF4.1代码中,如何覆盖Web.config中的ConnectionString名称
我正在创建一个多租户 Asp.Net MVC 3 Web 应用程序,并首先使用 EF4.1 代码作为数据库模型。
对于开发,我很高兴在 App_Data 中使用 SqlServerCE,对于生产,这将转移到 Sql Server 2008。
假设我的上下文称为“MyModels”,默认情况下代码优先会在 Web.config 中查找名为“MyModels”的连接字符串。这可以被告知使用App_Data中的文件或更改为访问SQL2008中的数据库。到目前为止一切都很好。
但由于多租户,我希望 SqlServerCE 文件名与租户的唯一 id 匹配(因此 App_Data 将具有“client_x.sdf”、“client_y.sdf”;Sql Server 2008 将具有单独的数据库)。我不知道如何定向到这些不同的数据库。
我尝试过 MyModels 继承自 DbContext 并提供连接字符串(在 Web.config 中使用“占位符”conn 字符串并用唯一 id 替换“{clientId}”),并且我还尝试在MyModels 构造函数:
base.Database.Connection.ConnectionString = xxx;
但这似乎永远不起作用。我总是收到以下错误:
发生网络相关或特定于实例的错误 建立与 SQL 的连接 服务器。找不到服务器或 无法访问。验证 实例名称正确且 SQL 服务器配置为允许远程 连接。 (提供者:命名管道 提供商,错误:40 - 无法打开 连接到 SQL Server)
(这表明它尚未“配置”为使用 SqlServerCE,因此正在尝试连接到 Sql Server。我想!)
跟踪代码,Database.Connection.ConnectionString 尚未从 Web.config 中读取此时,我无法搜索和替换它,而且它可能会被管道中的“占位符”连接字符串覆盖。
我认为这一定是非常简单的,但我只是找不到“钩子”。有人可以帮忙吗?
I'm creating a multi-tenant Asp.Net MVC 3 Web app, and using EF4.1 code first for the db model.
For development I'm happy to use SqlServerCE in App_Data, and for production this will move to Sql Server 2008.
Say my context is called "MyModels", by default code-first looks for a connection string called "MyModels" in Web.config. This can be told to use a file in App_Data or changed to access a database in SQL2008. All fine so far.
But because of multi-tenancy, I'd like the SqlServerCE filename to match the unique id of the tenant (so App_Data would have "client_x.sdf", "client_y.sdf"; Sql Server 2008 would have separate databases). I can't work out how to direct to these different databases.
I've tried MyModels inheriting from DbContext and supplying a connection string (using a 'placeholder' conn string in Web.config and replacing "{clientId}" with the unique id), and I've also tried setting the connection string in the MyModels constructor:
base.Database.Connection.ConnectionString = xxx;
but this never seems to work. I always get the following error:
A network-related or instance-specific error occurred while
establishing a connection to SQL
Server. The server was not found or
was not accessible. Verify that the
instance name is correct and that SQL
Server is configured to allow remote
connections. (provider: Named Pipes
Provider, error: 40 - Could not open a
connection to SQL Server)
(This suggests it's not yet 'configured' to use SqlServerCE, and so is trying to connect to Sql Server. I think!)
Tracing the code, Database.Connection.ConnectionString hasn't been read from Web.config at this point, so I can't search and replace that, and, possibly, it's getting overwritten by the 'placeholder' conn string later in the pipeline.
I reckon this must be quite straightforward and I just can't find the 'hook'. Can anyone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是解决您问题的可能方法。
我的想法是创建一个静态“工厂”类来实例化您的 DbContext 类。代码是这样的:
无论您需要实例化 MyModels 类,您都可以调用 CreateMyModels 方法:
当我需要在运行时根据某些逻辑更改连接字符串时,它对我有用。但我的 DbContext 类是由经典 EF 自动生成的,而不是代码优先。
I'm not sure if it's a possible approach to solve your problem.
My idea is to create a static "factory" class to instantiate your DbContext class. The code is like this:
And you may call CreateMyModels method wherever you need to instantiate MyModels class:
It works for me in the case I need to change the connection string according to some logic at runtime. But my DbContext class is generated automatically by classic EF, not code first.
我已经明白了,那是因为我有点笨。对不起!我只是想将 ConnectionString 设置为 Web.config 中的 ConnectionString,但实际上我需要使用 DbConnectionStringBuilder 来构建它。如果我替换构建器的 ConnectionString 属性,然后使用构建器设置 EF conn 字符串,它就可以工作。
I've figured it out, and it's because I'm a bit dumb. Sorry! I was just trying to set the ConnectionString to be the connectionString from Web.config, but I actually need to build it using the DbConnectionStringBuilder. If I replace the ConnectionString property of the builder, then use the builder to set the EF conn string, it works.