EntityFramework, AppHarbor and configuration variables

点点圈 提交于 2019-12-13 01:04:05

问题


I'm having some trouble with EntityFramework (database first) and AppHarbor.

I'm trying to use the configuration string as inserted into the web.config by AppHarbor (I've added the metadata into the Sequelizer config option on the website), and I'm trying to add some additional values using code provided.

Currently I'm being a very bad person and embedding the string directly into my apps configuration provider - not good if the hosting provider switch DBs on us, so I'm looking to do it the proper way and use the values AppHarbor supply via the web.config.

This is the string as per provided by AppHarbor (passwords and server details removed):

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>;'

If used "as is", that generates the following error:

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.

I then use the following code (purloined off of one of the AppHarbor support discussions) to append the required extra things EF needs...

          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;

That produces the connection string as follows:

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;

But that produces the error:

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

Index 165 being the start of "provider connection string".

The working connection string I use embedded, which currently works without issue, is:

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'

The only real differences being that the "multipleactiveresultsets=True;App=EntityFramework" entries are inside the "provider connection string" string rather than outside.

Other people seem to be using EntityFramework on AppHarbor using the supplied configuration variables fine, so what an I doing wrong?


回答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.



来源:https://stackoverflow.com/questions/10138285/entityframework-appharbor-and-configuration-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!