Search code examples
c#asp.net-mvcazureasp.net-web-api

The ConnectionString property has not been initialized on update-database


In package manager console I've run command update database against my database on azure, but I got error saying The ConnectionString property has not been initialized.

I did same thing against local database and everything went fine. In my web config I've changed connection string to mach my azure database and here is what my Database Context looks like:

 public class MadLabsDatabaseContext : IdentityDbContext<User>
{
    public MadLabsDatabaseContext()
        : base("DefaultConnection")
    {
        Configuration.LazyLoadingEnabled = true;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>()
           .ToTable("AspNetUsers");
        modelBuilder.Entity<User>()
            .ToTable("AspNetUsers");
    }
}

Here is Connection string in web.config:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=tcp:serverName.database.windows.net,1433;Database=madLabs_db;User ID=username;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

And here is my Package Manager console code:

Update-database -ConnectionString "Data Source=tcp:serverName.database.windows.net,1433;Database=madLabs_db;User ID=userName;Password=password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=True;" -ConnectionProviderName "System.Data.SqlClient"

Why am I getting this error ?


Solution

  • I think that I have answered this question here.

    I guess you have ASP Identity 2.0.0, for some reason this causes an issue on Azure but not on local, however the fix is easy.

    Try changing your code to:

    public class MadLabsDatabaseContext : IdentityDbContext<User>
    {
        // This is the offending line
        // Add throwIfV1Schema: false
        public MadLabsDatabaseContext() : base("DefaultConnection", throwIfV1Schema: false)
        {
            Configuration.LazyLoadingEnabled = true;
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            modelBuilder.Entity<IdentityUser>()
               .ToTable("AspNetUsers");
            modelBuilder.Entity<User>()
               .ToTable("AspNetUsers");
        }
    }
    

    Now try publishing to Azure, it should work.