3

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 ?

hyperN
  • 2,674
  • 9
  • 54
  • 92
  • Did you ever find the cause of this? – Alex Apr 16 '14 at 04:26
  • No, I din't, I've just created new project and did whole thing again same way and it worked, maybe I have had some stupid bug, or typo somewhere – hyperN Apr 16 '14 at 08:44
  • It was ASP Identity 2.0.0 that caused it. I bet in your new project you're now running 1.0.0. Check my answer for the solution (It literally takes 5 seconds to fix, but it took me hours to find it, and of course, it was right under my nose the entire time!) http://stackoverflow.com/a/23130143/2790000 – Alex Apr 17 '14 at 10:03

2 Answers2

15

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.

Community
  • 1
  • 1
Alex
  • 1,549
  • 2
  • 16
  • 41
  • 2
    Wow. You just saved me hours of late-night cursing and crying. I wouldn't have thought to try that in a million years. THANK YOU. – Gary McGill May 06 '14 at 22:38
  • @GaryMcGill I literally bashed my head against the desk for hours on this one - I couldn't find any docs about this change either. I'm glad it helped you out. – Alex May 07 '14 at 04:18
  • Unbelievable. I was literally pulling out what hair I still have. Thank you! – Trevor de Koekkoek Mar 18 '15 at 19:46
  • duude, this should be a sticky post just like in the old forum days. How you people know these kinds of "tricks" is beyond me, but glad you do! – mcy Aug 31 '15 at 10:47
1

It seems that public MadLabsDatabaseContext() : base("DefaultConnection") is not finding the correct connection string. I would step through that code to be sure that the context is being created with the connection string you expect.

This may help explain it How to fix "The ConnectionString property has not been initialized"

So use ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString instead of "DefaultConnection"

Community
  • 1
  • 1
Trisk
  • 593
  • 3
  • 8