实体框架MySql是否插入创建tmp表?

时间:2014-07-03 05:11:33

标签: c# mysql asp.net-identity entity-framework-6.1

我通过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用户没有&#39;超级&#39;数据库上的权限,因此迁移并未完全完成 - 即使该行已添加到__MigrationsHistory表中,表明如此...回滚迁移后,添加&#39; Super& #39;特权,然后重新进行迁移,一切正常。

0 个答案:

没有答案