ConnectionString属性尚未在update-database上初始化
在包管理器控制台中,我在azure上对我的数据库运行命令更新数据库,但是我收到错误,说ConnectionString属性尚未初始化。
我对本地数据库做了同样的事情,一切都很顺利。 在我的web配置中,我已经将连接字符串更改为mach我的azure数据库,这是我的数据库上下文的样子:
public class MadLabsDatabaseContext : IdentityDbContext { public MadLabsDatabaseContext() : base("DefaultConnection") { Configuration.LazyLoadingEnabled = true; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .ToTable("AspNetUsers"); modelBuilder.Entity() .ToTable("AspNetUsers"); } }
这是web.config中的连接字符串:
这是我的Package Manager控制台代码:
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"
为什么我收到此错误?
我想我已在这里回答了这个问题。
我猜你有ASP Identity 2.0.0 ,由于某些原因这会导致Azure上的问题而不是本地问题,但修复很容易。
尝试将代码更改为:
public class MadLabsDatabaseContext : IdentityDbContext { // 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() .ToTable("AspNetUsers"); modelBuilder.Entity() .ToTable("AspNetUsers"); } }
现在尝试发布到Azure,它应该工作。
public MadLabsDatabaseContext() : base("DefaultConnection")
似乎找不到正确的连接字符串。 我将逐步完成该代码,以确保使用您期望的连接字符串创建上下文。
这可能有助于解释它如何修复“ConnectionString属性尚未初始化”
所以使用ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString
而不是"DefaultConnection"
上述就是C#学习教程:ConnectionString属性尚未在update-database上初始化分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1011695.html