ConnectionString属性尚未在更新数据库初始化 [英] The ConnectionString property has not been initialized on update-database

查看:183
本文介绍了ConnectionString属性尚未在更新数据库初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在包管理器控制台,我对我的Azure数据库运行命令更新数据库,但我得到了错误说ConnectionString属性尚未初始化。

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.

我做同样的事情对本地数据库,一切都很好。在我的web配置我已经改变了连接字符串马赫我Azure数据库,这里是我的数据库上下文是什么样子:

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");
    }
}

下面是在web.config中的连接字符串:

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>

这是我的包管理器控制台code:

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 ?

推荐答案

我认为,我已经在这里回答了这个问题

I think that I have answered this question here.

我猜你的 ASP身份2.0.0 后,由于某种原因,这将导致在Azure上一个问题,但不是本地,但修复容易。

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.

试着改变你的code到:

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");
    }
}

现在尝试发布到Azure中,它应该工作。

Now try publishing to Azure, it should work.

这篇关于ConnectionString属性尚未在更新数据库初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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