2

I have an MVC5 / EF6.1 website that runs perfectly on my development machine using LocalDb.

However, when I publish this to an Azure Website with an Azure SQL Database, I get the following error when doing any database interaction:

The ConnectionString property has not been initialized.

I've searched all over and can't find the reason that this happens on Azure.

The first file the stack trace points to is IdentityModels.cs:45

That contains the following:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() : base("DefaultConnection")
    {

    }
}

When I publish to Azure I've tested the connection string (comes back OK), and this is the Settings screen:

enter image description here

Any idea as to what is going on?

UPDATE:

If don't select ApplicationDbContext and instead select DefaultConnection it works, however I won't be able to use code first migrations. How can I get the ApplicationDBContext to work again?

enter image description here

Alex
  • 1,549
  • 2
  • 16
  • 41

2 Answers2

20

For anyone else who runs into this problem - I found my solution.

I had just upgraded ASP Identity to version 2.0.0 via Nuget, and when it installed the Microsoft.AspNet.Identity.EntityFramework package it changed my publish settings and seperated the AppliationDbContext and the DefaultConnectionString and this is what caused the problem.

What I had to do was literally as easy as to change the following to my ApplicationDbContext

ASP Identity 1.0.0

public ApplicationDbContext() : base("DefaultConnection")
{

}

ASP Identity 2.0.0

// Set the throwIfV1Schema to false...
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{

}

I have no idea why this didn't affect my local development version, but it's solved my issue. Hope this helps someone!

Alex
  • 1,549
  • 2
  • 16
  • 41
  • 1
    Thanks a bunch man. I grappled with this issue all morning. I went through the same process as you followed. It was driving me crazy. – Mitch Labrador Apr 16 '14 at 15:50
  • This worked for me too, although my see method didn't fire (which it does on my local dev machine). Any thoughts? – Jason James May 13 '14 at 09:06
  • @JasonJames Are you 100% sure that you checked the "Execute Code First Migrations (runs on application start)." checkbox when you published? – Alex May 13 '14 at 09:16
  • When I added the extra argument I didn't even see the context used for Identity in the list of available databases when I go to publish it. I still see the other context, but that doesn't have a seed method. However, the schema was only created when I first tried to access the tables, not during publishing. – Jason James May 13 '14 at 09:23
  • @JasonJames It's hard to tell without seeing your project unfortunately, however if that context doesn't appear on the publish screen then something isn't set correctly. You should try adding "throwIfV1Schema: false" onto both of your contexts. Then in theory they should appear on the publish screen and allow you to execute your migrations and seed method. Good luck! :) – Alex May 13 '14 at 09:26
  • It seems to only be a parameter if the context inherits from IdentityDbContext, and not DbContext, so I can't add it to both. – Jason James May 13 '14 at 11:11
  • 1
    Thank you! You helped resolve what was a very frustrating problem for me! – Trevor de Koekkoek Nov 28 '14 at 15:27
-1

I just unclick "execute code first migration" it worked.

Amal Shalika
  • 1,077
  • 1
  • 13
  • 22