1

我在使用 EntityFramework(数据库优先)和 AppHarbor 时遇到了一些问题。

我正在尝试使用 AppHarbor 插入到 web.config 中的配置字符串(我已将元数据添加到网站上的 Sequelizer 配置选项中),并且我正在尝试使用提供的代码添加一些附加值。

目前我是一个非常糟糕的人,将字符串直接嵌入到我的应用程序配置提供程序中 - 如果托管服务提供商在我们身上切换数据库,那就不好了,所以我希望以正确的方式做到这一点并使用 AppHarbor 通过提供的值网络配置。

这是 AppHarbor 提供的字符串(已删除密码和服务器详细信息):

metadata='res://*/MyDataEntities.csdl|res://*/MyDataEntities.ssdl|res://*/MyDataEntities.msl;';provider=System.Data.SqlClient;provider connection string='Server=servername.sequelizer.com;Database=databasename;User ID=username;Password=<snip>;'

如果“按原样”使用,则会产生以下错误:

The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.

然后,我使用以下代码(从 AppHarbor 支持讨论之一中窃取)来附加 EF 需要的所需额外内容......

          if (String.IsNullOrWhiteSpace(ProductionDatabaseConnectionString))
            {
                // Get it on first read and cache it
                var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                var connectionString = configuration.ConnectionStrings.ConnectionStrings["SQLAppHarbor001"].ConnectionString;
                // Add the required extra metadata for EF4.x
                if (!connectionString.Contains("MultipleActiveResultSets=True;"))
                    connectionString += "MultipleActiveResultSets=True;";
                if (!connectionString.Contains("App=EntityFramework;"))
                    connectionString += "App=EntityFramework;";
                configuration.ConnectionStrings.ConnectionStrings["SQLAppHarbor001"].ConnectionString = connectionString;
                configuration.Save();
                ProductionDatabaseConnectionString = connectionString;
            }
            return ProductionDatabaseConnectionString;

这会产生如下连接字符串:

metadata='res://*/MyDataEntities.csdl|res://*/MyDataEntities.ssdl|res://*/MyDataEntities.msl;';provider=System.Data.SqlClient;provider connection string='Server=servername.sequelizer.com;Database=databasename;User ID=username;Password=<snip>;'MultipleActiveResultSets=True;App=EntityFramework;

但这会产生错误:

Format of the initialization string does not conform to specification starting at index 165.

索引 165 是“提供者连接字符串”的开始。

我使用嵌入的工作连接字符串,目前可以正常工作,是:

metadata='res://*/MyDataEntities.csdl|res://*/MyDataEntities.ssdl|res://*/MyDataEntities.msl;';provider=System.Data.SqlClient;provider connection string='Server=servername.sequelizer.com;Database=databasename;User ID=username;Password=<snip>;multipleactiveresultsets=True;App=EntityFramework'

唯一真正的区别是“multipleactiveresultsets=True;App=EntityFramework”条目在“provider connection string”字符串内部而不是外部。

其他人似乎在 AppHarbor 上使用提供的配置变量很好地使用 EntityFramework,那么我做错了什么?

4

1 回答 1

1

Update: Multiple Active Result Sets (MARS) can now be enabled for the injected connection string by using the Sequelizer admin panel. This is the recommended approach since the web.config no longer needs to be modified, which causes an AppDomain reload during startup

I came up against this today! I did the following:

var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var connectionString = configuration.ConnectionStrings.ConnectionStrings["ConnStringAlias"].ConnectionString;
if (!connectionString.Contains("multipleactiveresultsets=True;"))
{
    connectionString = connectionString.TrimEnd('\'');
    connectionString = connectionString += "multipleactiveresultsets=True;\'";
    configuration.ConnectionStrings.ConnectionStrings["ConnStringAlias"].ConnectionString = connectionString;
    configuration.Save();
}

The MultipleActiveResultSets property must be inside the provider connection string, which is why you received an error regarding the format of your connection string.

I seen a few 'solutions' around but none seemed to work for me, including the solution at the bottom of a support page of how to do exactly this on AppHarbor's site. The solution provided even sends the application into an infinite loop as the application will restart every time the web.config file is saved, which is every time in the example.

于 2012-09-11T21:00:39.567 回答