从Azure数据库中获取IdentityUser数据时出错

时间:2016-04-16 10:49:34

标签: entity-framework azure

在将项目发布到Azure App Service之前,我在Visual Studio中执行了代码优先迁移,没有任何种子数据。创建了6个表:

  1. dbo._MigrationHistory
  2. dbo.AspNetRoles
  3. dbo.AspNetUserClaims
  4. dbo.AspNetUserLogins
  5. dbo.AspNetUserRoles
  6. dbo.AspNetUsers
  7. 当我尝试通过向控制器发送请求来查找是否已注册某些IdentityUser时,我遇到以下错误:

    2016-04-16T09:58:30  PID[6608] Error
       at System.Data.SqlClient.SqlConnection.PermissionDemand()
       at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.Open()
       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 authentication.Contexts.AuthContext..ctor() in d:\Projects\learnAuth\authentication\Contexts\AuthContext.cs:line 7
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
       at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState)
       at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
       at System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)
       at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
       at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
       at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
       at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
       at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
       at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
       at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
       at System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)
       at System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync[TSource](IQueryable`1 source, Expression`1 predicate)
       at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<FindAsync>d__23.MoveNext()
    

    Web.config中的连接字符串:

    <connectionStrings>
        <add name="AuthContext" connectionString="Server=tcp:seraphtest.database.windows.net,1433;Database=learnauth;User ID=seraph@seraphtest;Password=XXXXXXXX;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    AuthContext.cs

    public class AuthContext : IdentityDbContext<IdentityUser>
    {
        public AuthContext() : base("AuthContext") { }
    }
    

    错误来源:

    private UserManager<IdentityUser> userManager;
    
    public async Task<IdentityUser> Find(UserLoginInfo loginInfo)
    {
        // stack trace starts from here
        IdentityUser user = await userManager.FindAsync(loginInfo);
        return user;
    }
    

    此错误的来源是什么?我该如何解决?

1 个答案:

答案 0 :(得分:0)

如果我从

更改连接字符串,结果证明是有效的
<connectionStrings>
    <add name="AuthContext" connectionString="Server=tcp:seraphtest.database.windows.net,1433;Database=learnauth;User ID=seraph@seraphtest;Password=XXXXXXXX;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient" />
</connectionStrings>

<connectionStrings>
    <add name="AuthContext" connectionString="Server=tcp:seraphtest.database.secure.windows.net,1433;Database=learnauth;User ID=seraph@seraphtest;Password=XXXXXXXX;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;" providerName="System.Data.SqlClient" />
</connectionStrings>