我正在尝试执行代码优先实体框架迁移。 但是当我尝试运行时,我遇到连接字符串错误。
我的网络配置文件中的连接字符串是
<connectionStrings>
<add name="AuthContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=owinAuthentication;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
我的AuthContext.cs是
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace owinAuthentication
{
public class AuthContext : IdentityDbContext<IdentityUser>
{
public AuthContext()
: base("AuthContext")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<AuthContext, Migrations.Configuration>(ConfigurationManager.ConnectionStrings["AuthContext"].ConnectionString));
}
public DbSet<Client> Clients { get; set; }
public DbSet<RefreshToken> RefreshTokens { get; set; }
}
}
然而,当我运行我的应用程序时,我正在接收。
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=No connection string named 'Data Source=(localdb)\v11.0;Initial Catalog=owinAuthentication;Integrated Security=True' could be found in the application config file.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Infrastructure.DbConnectionInfo.GetConnectionString(AppConfig config)
at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
at System.Data.Entity.Internal.LazyInternalContext.OverrideConnection(IInternalConnection connection)
at System.Data.Entity.Infrastructure.DbContextInfo.ConfigureContext(DbContext context)
at System.Data.Entity.Internal.InternalContext.ApplyContextInfo(DbContextInfo info)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.CreateObjectContextForDdlOps()
at System.Data.Entity.Database.Exists()
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1.IsIdentityV1Schema(DbContext db)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1..ctor(String nameOrConnectionString, Boolean throwIfV1Schema)
at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1..ctor(String nameOrConnectionString)
at owinAuthentication.AuthContext..ctor() in C:\Users\delli\Documents\Visual Studio 2015\Projects\owinAuthentication\owinAuthentication\Models\AuthContext.cs:line 15
InnerException:
查看其他堆栈溢出答案我尝试更改
: base("AuthContext")
到
: base(ConfigurationManager.ConnectionStrings["AuthContext"].ConnectionString));
但仍然收到错误。