“The ConnectionString property has not been initia

2019-05-21 11:17发布

问题:

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:

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?

回答1:

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!



回答2:

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