带有实体框架6的mysql 5.7 - 用户'root'@'localhost'的访问被拒绝(使用密码:NO)

Wil*_*ong 1 c# mysql entity-framework asp.net-mvc-4

我目前正在开发一个包含Entity Framework 6和MySQL数据库的MVC项目.

我已经安装了以下内容:

  • MySQL.Data v6.9.9
  • MySQL.Data.Entity v6.9.9
  • 实体框架v6.1.3

首先,我在web.config中添加了以下连接字符串: -

<connectionStrings>
  <add name="MySQLContext" connectionString="server=localhost;database=db;uid=root;password=xxxxxxxx" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Run Code Online (Sandbox Code Playgroud)

然后我添加了以下内容: -

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>                            
</providers></entityFramework>
Run Code Online (Sandbox Code Playgroud)

我在我的模型中添加了这个上下文:

    public class UsersContext : DbContext
    {
        public UsersContext()
            : base()
        {
        }

        // Constructor to use on a DbConnection that is already opened
        public UsersContext(DbConnection existingConnection, bool contextOwnsConnection)
           : base(existingConnection, contextOwnsConnection)
        {

        }

        public DbSet<LoginUsers> LoginUsers { get; set; }
    }

    [Table("login_users")]
    public class LoginUsers
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public DateTime Created_At { get; set; }
        public bool Active { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

然后当我尝试注册/添加用户时(仅限演示):

private static string connectionString = ConfigurationManager.ConnectionStrings["MySQLContext"].ConnectionString;

    public static bool RegisterUser(string name, string username, string password, out string errorMessage)
    {
        string hashedPassword = SHA256Encrypt(password);

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            connection.Open();
            MySqlTransaction transaction = connection.BeginTransaction();

            try
            {
                // DbConnection that is already opened
                using (UsersContext context = new UsersContext(connection, false))
                {
                    // Passing an existing transaction to the context
                    context.Database.UseTransaction(transaction);

                    context.LoginUsers.Add(new LoginUsers()
                    {
                        Name = name,
                        Username = username,
                        Password = hashedPassword,
                        Created_At = DateTime.UtcNow,
                        Active = true
                    });
                    context.SaveChanges();
                }

                errorMessage = "";
                transaction.Commit();
                return true;
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                errorMessage = "Contain error";
                return false;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

所有模型和连接字符串都已正确.连接字符串也适用于传统的MySQLCommand.但是,当代码到达以下部分时:

context.LoginUsers.Add(new LoginUsers()
                    {
                        Name = name,
                        Username = username,
                        Password = hashedPassword,
                        Created_At = DateTime.UtcNow,
                        Active = true
                    });
Run Code Online (Sandbox Code Playgroud)

它引发了以下异常:

在此输入图像描述

在此输入图像描述

我也为我的连接字符串设置了正确的密码.我还使用'root'@'localhost'为我的数据库添加了grant权限.

在此输入图像描述

我现在卡在这一部分.在互联网和SO搜索解决方案,但没有一个工作.任何人都面临这个问题,很少有人可以自由地检查可能的原因以及如何解决它?

感谢所有能够提供帮助的人!

Wil*_*ong 5

我自己找到了解决方案.在尝试和错误之后,事实证明您在连接字符串中需要"persistsecurityinfo = True"才能使其正常工作.