ASP.NET MVC 4 - Application not accepting new connection string - AppHarbor Deploy Issue

10,081

You have to specify the name of the connectionstring you want your DbContext to load. It has no way to magically guess that you want it to use the one called DefaultConnection. There's a heuristic that says that if no name is specified, it'll look for for a connectionstring with name set to the name of the class that inherits from DbContext. Ie. if you have:

MyAwesomeDatebase : DbContext

... then Entity Framework will work out of the box with this:

<add name="MyAwesomeDatebase" connectionString="blah" providerName="System.Data.SqlClient" />

... but if you have:

<add name="DefaultConnection" connectionString="blah" providerName="System.Data.SqlClient" />

... then it won't work because Entity Framework has no way of knowing that MyAwesomeDatebase goes with DefaultConnection.

To make that work, do this:

public class MyAwesomeDatebase : DbContext
{
    public MyAwesomeDatebase() : base("DefaultConnection")
}

... and you're golden.

Share:
10,081
PhillipKregg
Author by

PhillipKregg

I enjoy doing fun things with JavaScript, Ruby, Rails, and C# with ASP.NET MVC. I've also got a good bit of experience in UI/UX design and graphic design - if there were enough hours in the day I'd like to dive into some Django too. ... And every now and then I'm known to partake of some funky bass playing.

Updated on June 26, 2022

Comments

  • PhillipKregg
    PhillipKregg almost 2 years

    I'm currently working on an ASP.NET MVC 4 project that is using Entity Framework 4.3 with a CodeFirst approach using simple POCO classes. Also, I'm using SQL Server Express as the development database.

    After building my classes and setting the connection string, I ran my project and it generated a SQL Server Express database for me with no problems.

    The problem though, is that I am trying to deploy to AppHarbor and I'm having an issue with the connection string. AppHarbor requires that you install SQL Server as an 'Add-On' and configure the connection string to have an alias that will inject their Sequilizer connection string into the project that you push from GitHub.

    Here is their documentation on how this works: http://support.appharbor.com/kb/add-ons/using-sequelizer

    I believe I have all of this setup correctly, but there seems to be a problem with how my app is reading the connection string.

    Here is the development connection string that I am using on my local machine:

    <connectionStrings>
        <add name="DefaultConnection" 
             connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-FranchiseManager-201275154247;Integrated Security=True" 
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    Here is what the AppHarbor Sequilizer connection string looks like:

    <connectionStrings>    
        <add
          name="DefaultConnection"
          connectionString="Server=0691298f-d461-40cc-86d6-a089016ba88d.sqlserver.sequelizer.com;Database=<removed hash value>;User ID=<removed hash value>;Password=<removed hash value>;"
          providerName="System.Data.SqlClient"
        />
      </connectionStrings>
    

    The first connection - generated locally by my EF - works just fine. The second - created by Sequilizer - is not being read by my application.

    However - I can connect to the Sequilizer database through SQL Server Management Studio. So it must be my app right?

    In order to trouble shoot my deployment to AppHarbor, I hard-coded their connection string into my app instead of the one auto-generated by EF and tested on my local machine.

    Here's what I did:

    1. Replaced the connection string in Web.config with the one AppHarbor provided,
    2. Cleaned the solution,
    3. Rebuilt the solution

    But when I ran the application, it is still utilizing the original database generated by EF - which must mean that it is still reading the old connection string.

    It seems like changing the connection string is not enough. What else in my application should I change in order to replace the connection string?

    Any advice is appreciated - thanks!


    EDIT


    Here is my DbContext class:

    public class FranchiseManagerContext : DbContext
        {
            public DbSet<FranchiseSet> FranchiseSets { get; set; }
            public DbSet<Franchise> Franchises { get; set; }
    
        }
    

    This works as-is with the connection string named "DefaultConnection".

    How does EF know to match the DbContext with the connection string in this scenario, but it cannot do it if you change the name of the string?

    UPDATE

    I think I know what it is now from this SO answer: What is the point of "Initial Catalog" in a SQL Server connection string?

    It looks like the Initial Catalog attribute is specifying the particular database to be used when Entity Framework first kicked in.