0

I am attempting to do a code first entity framework migration. however I am hitting a connection string error when I attempt to run.

my connection string in my web config file is

 <connectionStrings>
    <add name="AuthContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=owinAuthentication;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

my AuthContext.cs is

using System.Data.Entity;
using System.Linq;
using System.Web;

namespace owinAuthentication
{
    public class AuthContext : IdentityDbContext<IdentityUser>
    {
        public AuthContext()  
            : base("AuthContext")
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<AuthContext, Migrations.Configuration>(ConfigurationManager.ConnectionStrings["AuthContext"].ConnectionString));
        }
        public DbSet<Client> Clients { get; set; }
        public DbSet<RefreshToken> RefreshTokens { get; set; }
    }
}

however when I run my app I am receiving.

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=No connection string named 'Data Source=(localdb)\v11.0;Initial Catalog=owinAuthentication;Integrated Security=True' could be found in the application config file.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Infrastructure.DbConnectionInfo.GetConnectionString(AppConfig config)
       at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
       at System.Data.Entity.Internal.LazyInternalContext.OverrideConnection(IInternalConnection connection)
       at System.Data.Entity.Infrastructure.DbContextInfo.ConfigureContext(DbContext context)
       at System.Data.Entity.Internal.InternalContext.ApplyContextInfo(DbContextInfo info)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       at System.Data.Entity.Internal.InternalContext.CreateObjectContextForDdlOps()
       at System.Data.Entity.Database.Exists()
       at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1.IsIdentityV1Schema(DbContext db)
       at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1..ctor(String nameOrConnectionString, Boolean throwIfV1Schema)
       at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1..ctor(String nameOrConnectionString)
       at owinAuthentication.AuthContext..ctor() in C:\Users\delli\Documents\Visual Studio 2015\Projects\owinAuthentication\owinAuthentication\Models\AuthContext.cs:line 15
  InnerException: 

looking at other stack overflow answers I tried changing

 : base("AuthContext")

to

: base(ConfigurationManager.ConnectionStrings["AuthContext"].ConnectionString));

but still receiving the error.

Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
  • did you install the `.NET framework 4 update 4.0.2` – MethodMan Jul 11 '17 at 01:58
  • Possible duplicate of [What is the connection string for localdb for version 11](https://stackoverflow.com/questions/10540438/what-is-the-connection-string-for-localdb-for-version-11) – MethodMan Jul 11 '17 at 01:59
  • you want to migrate current database to another database using **migration** feature? – Lei Yang Jul 11 '17 at 02:01
  • First, I would move SetIntiializer to a [static constructor](https://stackoverflow.com/questions/16490087/database-setinitializer-in-a-static-constructor) since it is static. That's probably not your issue though. What is the state of this app and database? Does the database already exist? If not, comment out the initializer and see if EF creates it. – Steve Greene Jul 11 '17 at 13:23
  • It already exists. I added some columns and cred to run a migration to add the columns to the db – Bryan Dellinger Jul 11 '17 at 15:50

0 Answers0