0

我通过 ASP.Net Identity 2.0 框架使用 EF 6.1 CodeFirst。我扩展了DBContext包含一个新表,用于存储对我的应用程序的邀请:

public class MySqlDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    <snip/>

    /// <summary>
    /// Gets or sets the database set for user invites.
    /// </summary>
    public IDbSet<ApplicationInvite> Invites { get; set; }

    <snip/>
}

并利用迁移框架创建迁移以添加Invites表。

这是我用来插入的代码:

public class InviteStore : IInviteStore
{
    private readonly MySqlDbContext mySqlDbContext;
    private readonly IDbSet<ApplicationInvite> invites;

    public InviteStore(MySqlDbContext mySqlDbContext)
    {
        this.mySqlDbContext = mySqlDbContext;
        this.invites = this.mySqlDbContext.Set<ApplicationInvite>();
    }

    public async Task<bool> CreateAsync(ApplicationInvite invite)
    {
        this.invites.Add(invite);
        await this.mySqlDbContext.SaveChangesAsync().ConfigureAwait(false);
        return true;
    }
}

我现在看到的是尝试插入Invites表时抛出的异常。我不明白这Table 'MyDatabase.tmpIdentity_Invites' doesn't exist是从哪里来的。

{
  "exceptionMessage": "An error occurred while updating the entries. See the inner exception for details.",
  "exceptionType": "System.Data.Entity.Infrastructure.DbUpdateException",
  "innerException": {
    "message": "An error has occurred.",
    "exceptionMessage": "An error occurred while updating the entries. See the inner exception for details.",
    "exceptionType": "System.Data.Entity.Core.UpdateException",
    "stackTrace": "   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<UpdateAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Core.Objects.ObjectContext.
    <ExecuteInTransactionAsync>d__3d`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Core.Objects.ObjectContext.
        <SaveChangesToStoreAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Data.Entity.Core.Objects.ObjectContext.
            <SaveChangesInternalAsync>d__31.MoveNext()",
    "innerException": {
      "message": "An error has occurred.",
      "exceptionMessage": "Table 'MyDatabase.tmpIdentity_Invites' doesn't exist",
      "exceptionType": "MySql.Data.MySqlClient.MySqlException",
      "stackTrace": "   at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
   at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
   at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
   at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
   at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.
                <ExecuteAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.
                    <UpdateAsync>d__0.MoveNext()"
    }
  }
}

有人可以帮忙吗?

谢谢。

编辑 看起来根本原因是迁移没有成功完成。它在抛出:

You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

我发现根本原因是实例化迁移的 MySQL 用户在数据库上没有“超级”权限,因此迁移并未完全完成——即使该行已添加到__MigrationsHistory表中表明如此.. . 回滚迁移后,添加“超级”权限,然后重新进行迁移,一切正常。

4

0 回答 0