“ConnectionString 属性尚未初始化."- 但仅在发布时 [英] "The ConnectionString property has not been initialized." - but only when publishing

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

问题描述

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

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

但是,当我将其发布到带有 Azure SQL 数据库的 Azure 网站时,在进行任何数据库交互时都会收到以下错误:

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 时,我已经测试了连接字符串(返回正常),这是设置屏幕:

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

知道发生了什么吗?

更新:

如果不选择 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 Identity 升级到 2.0.0 版,当它安装 Microsoft.AspNet.Identity.EntityFramework 包时,它更改了我的发布设置和将 AppliationDbContextDefaultConnectionString 分开,这就是导致问题的原因.

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 身份 1.0.0

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

}

ASP 身份 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 属性尚未初始化."- 但仅在发布时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆