Question

Using vs2012, I've got a Test Unit project (for testing a Service) that incorporates an .edmx file and linq. The edmx is created at design time and I have created an object (called Store.Data.Common) that retrieves the connection string from the App.Config file (decrypts the string and builds the entire string including the meta data):

//Object is called Store.Data.Common
public static string GetConnectionString(string databaseName)
{
    var security = new Security();

    var connectionString = security.GetDecoded(ConfigurationManager.ConnectionStrings[databaseName+"Encrypted"].ToString(), 0);
    var environment = ConfigurationManager.AppSettings["Environment"].ToString();
    var dataSource = security.GetDecoded(ConfigurationManager.AppSettings[environment], 0);

    connectionString = string.Format(connectionString, dataSource);

    return connectionString;
}

I've also modified the .tt files to include an overload of the constructor to call this method to build the connection string, like so:

//Original Constructor which I modified and added the parameter to pass to the other constructor.
public StoreContext()
 : base("name=StoreContext")
{
}

//Constructor I added:
public StoreContext(string connection)
{
    Store.Data.Common.ConnectionBuilder.GetConnectionString("StoreContext");
}

Everything builds correctly, however, when I try to new-up an object for the StoreContext and leave the constructor empty, it never gets to the second constructor:

StoreContext storeContext = new StoreContext();

When I debug this test and walk through it, it only gets to the first constructor and that's it. Obviously, if I do something like this:

StoreContext storeContext = new StoreContext("Blah");

Then it goes to the second one as expected....by my question is, why doesn't the first method work when passing nothing to the constructor? Technically it should work, right?

Was it helpful?

Solution

I think you mean to use

public StoreContext()
  : this("name=StoreContext")
{
}

(using this rather than base).

using this() means you're calling a constructor on the same class. When you say base() you're trying to call a constructor on the base class.

Edit: It also doesn't look like you're using the parameter you're passing into that non-default constructor. But that's another issue, not the root of your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top