Search code examples
entity-frameworkwcf-data-services

CreateDataSource not working with WCF and a stored procedure


I am trying to override the connection string in a WCF DataService with CreateDataSource. This seems to work fine for simple requests for tables, but when I try to use a stored procedure it fails...it's always trying to use the original datasource set up for the SP when i worked with the .edmx file.

My code is:

string mConnectionString; 
mConnectionString = ConfgurationManager.ConnectionStrings["D7SG_DEVEntities"].ConnectionString;
return new SGEntities(mConnectionString);`

The error which occurs is

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Anyone know how to make this work with a stored procedure ? (This procedure works fine when used with the hard-coded string in web.config)


Solution

  • In your service operation, you're creating the SGEntities without passing in the modified connection string. So it will pick the default connection string from your configuration. In order for this to work you can use this.CurrentDataSource instead of creating a new instance of the SGEntities in your service operation implementation. That will go through the CreateDataSource and should pick up the new connection string.