Scenario: I want to dynamically choose the connection string based on the environment that the application is running in.
Environment: I have a two environments Dev and Production for a WCF Service. I store the connection strings in the web.config. When that app starts up set a variable with the correct connection string to use.
Problem: When the data model is generated it creates 2 files dmMyDataModel.edmx and dmMyDataModel.Designer.cs, in the designer file there are constructors in there that define the connection to the EntityDatabas.
In the code below you can see that I have created a constructor in the service that when the service is called it will set the context to use the right connection string. But only the items that use myContext work correctly (the stored procedures) the querying of the dbMyWebSiteEntities fail in production environment because the still depend on the connection string set in the designer.
I tired setting the logic in the designer but that get wiped out when its re-generated.
dmMyDataModel.Designer.cs
using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;
[assembly: EdmSchemaAttribute()]
namespace MyWebSite.Services
{
#region Contexts
/// <summary>
/// No Metadata Documentation available.
/// </summary>
public partial class dbMyWebSiteEntities : ObjectContext
{
#region Constructors
/// <summary>
/// Initializes a new dbMyWebSiteEntities object using the connection string found in the 'dbMyWebSiteEntities' section of the application configuration file.
/// </summary>
public dbMyWebSiteEntities() : base("name=dbMyWebSiteEntities", "dbMyWebSiteEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
/// <summary>
/// Initialize a new dbMyWebSiteEntities object.
/// </summary>
public dbMyWebSiteEntities(string connectionString) : base(connectionString, "dbMyWebSiteEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
/// <summary>
/// Initialize a new dbMyWebSiteEntities object.
/// </summary>
public dbMyWebSiteEntities(EntityConnection connection) : base(connection, "dbMyWebSiteEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
#endregion
...
}
MyWebSiteData.svc.cs
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.ServiceModel.Activation;
using System.ServiceModel;
using System.Data.EntityClient;
using System.Configuration;
using System.Data.Objects;
namespace MyWebSite.Services
{
public class MyWebSiteData : DataService<dbMyWebSiteEntities>
{
private dbMyWebSiteEntities myContext;
public static void InitializeService(DataServiceConfiguration config)
{
config.UseVerboseErrors = true;
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetEntitySetPageSize("*", 25);
config.SetServiceOperationAccessRule("mar_getAllByKey", ServiceOperationRights.AllRead);
}
//Constructor to override the connection string used in the dmMyDataModel.Designer.cs
public MyWebSiteData()
{
//sets the connetion string and appends the environment so it will pull the correct one from the web.config
EntityConnection conn = new EntityConnection("name=dbMyWebSiteEntities" + HttpContext.Current.Application["Environment"]);
myContext = new dbMyWebSiteEntities(conn);
}
//This returns the data from the stored procedures
[WebGet]
public ObjectResult<myTable> mar_getAllByKey(string key)
{
return myContext.mar_getAllByKey(key);
}
}
}
Thanks for taking the time to look at this. I have tried to be specific and detailed but if I left something out let me know.
Thanks, Gerald