本文介绍了“ConnectionString属性尚未初始化”。 - 但只在发布时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC5 / EF6.1网站,在我的开发机器上使用LocalDb完美运行。

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

但是,当我将它发布到Azure Azure Azure SQL数据库,当执行任何数据库交互时,我收到以下错误:

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

ConnectionString属性尚未初始化。

我已经搜索过,找不到在Azure上发生的原因。

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

堆栈跟踪指向的第一个文件是 IdentityModels.cs:45

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

包含以下内容:

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

    }
}

当我发布到Azure我已经测试了连接字符串(返回OK),这是设置屏幕:

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?

更新:

如果不选择 ApplicationDbContext ,而是选择 DefaultConnection 它可以工作,但是我将无法使用代码首次迁移。如何获得 ApplicationDBContext 再次工作?

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?

推荐答案

问题 - 我找到了我的解决方案。

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

我刚刚通过Nuget将 ASP身份升级到2.0.0版本,当它安装 Microsoft.AspNet.Identity.EntityFramework 包更改了我的发布设置,并分开了 AppliationDbContext DefaultConnectionString 这是什么导致的问题。

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.

我必须做的是简单的将以下内容更改为我的 ApplicationDbContext

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!

这篇关于“ConnectionString属性尚未初始化”。 - 但只在发布时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 07:22
查看更多