locked
Database connection string error after changing DbContext RRS feed

  • Question

  • User233854513 posted

    I am getting this error when I run the project from my workstation. Interesting thing is that, it was working prior to setting the database initializer (to turn on EF code-first auto migration).
    No connection string named 'name=POC_ManagerContext' could be found in the application config file.

    Please see below for settings

    Working

    namespace POC_Manager.Models
    {
        public class POC_ManagerContext : DbContext
        {
            // You can add custom code to this file. Changes will not be overwritten.
            // 
            // If you want Entity Framework to drop and regenerate your database
            // automatically whenever you change your model schema, please use data migrations.
            // For more information refer to the documentation:
            // http://msdn.microsoft.com/en-us/data/jj591621.aspx
        
            public POC_ManagerContext() : base("name=POC_ManagerContext")
            {
            }
    
            public System.Data.Entity.DbSet<POC_Manager.Models.RequestsQueueObject> RequestsQueueObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.BookingObject> BookingObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.PODObject> PODObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.UseCaseObject> UseCaseObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.GlobalSettingsObject> GlobalSettingsObjects { get; set; }
        }
    }
        public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                //Database.SetInitializer<POC_ManagerContext>(new DropCreateDatabaseIfModelChanges<POC_ManagerContext>());
                Database.SetInitializer<POC_ManagerContext>(new CreateDatabaseIfNotExists<POC_ManagerContext>());
            }
        }
    <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=301879
      -->
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="POC_ManagerContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=POC_ManagerContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|POC_ManagerContext.mdf" providerName="System.Data.SqlClient" />
        <!--<add name="POC_ManagerContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=POC_ManagerContext-20140302173647; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|POC_ManagerContext-20140302173647.mdf" providerName="System.Data.SqlClient" />-->
      </connectionStrings>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <authentication mode="Windows" />
      
        <authorization>
          <deny users="?" />
        </authorization>
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

    Not Working

     public class POC_ManagerContext : DbContext
        {
            // You can add custom code to this file. Changes will not be overwritten.
            // 
            // If you want Entity Framework to drop and regenerate your database
            // automatically whenever you change your model schema, please use data migrations.
            // For more information refer to the documentation:
            // http://msdn.microsoft.com/en-us/data/jj591621.aspx
        
            public POC_ManagerContext() : base("name=POC_ManagerContext")
            {
                Database.SetInitializer(new MigrateDatabaseToLatestVersion<POC_ManagerContext, POC_Manager.Migrations.Configuration>("name=POC_ManagerContext"));
            }
    
            public System.Data.Entity.DbSet<POC_Manager.Models.RequestsQueueObject> RequestsQueueObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.BookingObject> BookingObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.PODObject> PODObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.UseCaseObject> UseCaseObjects { get; set; }
            public System.Data.Entity.DbSet<POC_Manager.Models.GlobalSettingsObject> GlobalSettingsObjects { get; set; }
            
        }
     public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                //Database.SetInitializer<POC_ManagerContext>(new DropCreateDatabaseIfModelChanges<POC_ManagerContext>());
                //Database.SetInitializer<POC_ManagerContext>(new CreateDatabaseIfNotExists<POC_ManagerContext>());
            }
        }
    <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=301879
      -->
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="POC_ManagerContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=POC_ManagerContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|POC_ManagerContext.mdf" providerName="System.Data.SqlClient" />
        <!--<add name="POC_ManagerContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=POC_ManagerContext-20140302173647; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|POC_ManagerContext-20140302173647.mdf" providerName="System.Data.SqlClient" />-->
        <add name="POC_ManagerContext_DatabasePublish" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=POC_ManagerContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|POC_ManagerContext.mdf" providerName="System.Data.SqlClient" />
      </connectionStrings>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <authentication mode="Windows" />
      
        <authorization>
          <deny users="?" />
        </authorization>
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
        <contexts>
          <context type="POC_Manager.Models.POC_ManagerContext, POC_Manager">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[POC_Manager.Models.POC_ManagerContext, POC_Manager], [POC_Manager.Migrations.Configuration, POC_Manager]], EntityFramework, PublicKeyToken=b77a5c561934e089">
              <parameters>
                <parameter value="POC_ManagerContext_DatabasePublish" />
              </parameters>
            </databaseInitializer>
          </context>
        </contexts>
      </entityFramework>
    </configuration>


    Friday, July 25, 2014 2:56 PM

All replies

  • User233854513 posted

    Interesting thing is that, it's working in production. Here is the webconfig.

    <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=301879
      -->
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="POC_ManagerContext" connectionString="Data Source=hostname.domain.com\SQL2012;Initial Catalog=POCManager;Integrated Security=True" providerName="System.Data.SqlClient" />
        <!--<add name="POC_ManagerContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=POC_ManagerContext-20140302173647; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|POC_ManagerContext-20140302173647.mdf" providerName="System.Data.SqlClient" />-->
        <add name="POC_ManagerContext_DatabasePublish" connectionString="Data Source=hostname.domain.com\SQL2012;Initial Catalog=POCManager;Integrated Security=True" providerName="System.Data.SqlClient" />
      </connectionStrings>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web>
        <compilation targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <authentication mode="Windows" />
      
        <authorization>
          <deny users="?" />
        </authorization>
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.10.0" newVersion="4.0.10.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
        <contexts>
          <context type="POC_Manager.Models.POC_ManagerContext, POC_Manager">
            <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[POC_Manager.Models.POC_ManagerContext, POC_Manager], [POC_Manager.Migrations.Configuration, POC_Manager]], EntityFramework, PublicKeyToken=b77a5c561934e089">
              <parameters>
                <parameter value="POC_ManagerContext_DatabasePublish" />
              </parameters>
            </databaseInitializer>
          </context>
        </contexts>
      </entityFramework>
    </configuration>

    Friday, July 25, 2014 3:12 PM
  • User233854513 posted

    On my workstation ... If i comment out the database initializer, it works!  How can I use EF code-first auto database migration and have it to work?

        public POC_ManagerContext() : base("name=POC_ManagerContext")
            {
              //  Database.SetInitializer(new MigrateDatabaseToLatestVersion<POC_ManagerContext, POC_Manager.Migrations.Configuration>("name=POC_ManagerContext"));
            }

    Friday, July 25, 2014 4:48 PM
  • User697462465 posted

    Hi zoolander,

    Based on your error message, I think the name=POC_ManagerContext as part of the connection string name.

    So please try to remove the name= from your structure function like this:

    public POC_ManagerContext() : base("POC_ManagerContext")
            {
               Database.SetInitializer(new MigrateDatabaseToLatestVersion<POC_ManagerContext, POC_Manager.Migrations.Configuration>("POC_ManagerContext"));
            }

    Best Regards,
    Terry Guo

    Monday, July 28, 2014 2:50 AM
  • User-1905614090 posted

    Is there another to deal with this? 

    I'm asking because I have a constructor that takes the connection string as "name=<connectionStringName>". 

    Monday, June 15, 2015 6:08 PM