gpt4 book ai didi

c# - 更新数据库上的 ConnectionString 属性尚未初始化

转载 作者:行者123 更新时间:2023-11-30 13:14:54 29 4
gpt4 key购买 nike

在包管理器控制台中,我已针对 azure 上的数据库运行命令更新数据库,但出现错误,提示 ConnectionString 属性尚未初始化。

我对本地数据库做了同样的事情,一切都很顺利。在我的网络配置中,我更改了连接字符串以匹配我的 azure 数据库,这是我的数据库上下文的样子:

 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 中的连接字符串:

<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>

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

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"

为什么我会收到此错误?

最佳答案

我想我已经回答了这个问题here.

我猜您有ASP Identity 2.0.0,由于某种原因,这会导致 Azure 上出现问题,但本地不会出现问题,但修复很容易。

尝试将代码更改为:

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,它应该可以工作。

关于c# - 更新数据库上的 ConnectionString 属性尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22865584/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com