mysql 5.7与实体框架6 - 访问被拒绝用户'root'@'localhost'(使用密码:否) [英] mysql 5.7 with entity framework 6 - Access denied for user 'root'@'localhost' (using password: NO)

查看:132
本文介绍了mysql 5.7与实体框架6 - 访问被拒绝用户'root'@'localhost'(使用密码:否)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6和MySQL数据库开发一个MVC项目。



我已经安装了以下内容:




  • MySQL.Data v6.9.9

  • MySQL.Data.Entity v6.9.9

  • 实体框架v6.1.3



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

 < connectionStrings> 
< add name =MySQLContextconnectionString =server = localhost; database = db; uid = root; password = xxxxxxxxproviderName =MySql.Data.MySqlClient/>
< / connectionStrings>

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

 < entityFramework codeConfigurationType =MySql.Data.Entity.MySqlEFConfiguration,MySql.Data.Entity.EF6> 
< defaultConnectionFactory type =System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework/>
< providers>
< provider invariantName =MySql.Data.MySqlClienttype =MySql.Data.MySqlClient.MySqlProviderServices,MySql.Data.Entity.EF6/>
< provider invariantName =System.Data.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>< / entityFramework>

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

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

//在已经打开的DbConnection上使用的构造方法
public UsersContext(DbConnection existingConnection,bool contextOwnsConnection)
:base(existingConnection,contextOwnsConnection)
{

}

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

[表(login_users)]
public class LoginUsers
{
public int Id {get;组; }
public string Username {get;组; }
public string Password {get;组; }
public string Name {get;组; }
public DateTime Created_At {get;组; }
public bool Active {get;组; }
}

然后,当我尝试注册/添加一个用户(仅演示):

  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();

尝试
{
//已经打开的DbConnection
使用(UsersContext context = new UsersContext(connection,false))
{
//将现有事务传递给上下文
context.Database.UseTransaction(transaction);

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

errorMessage =; 200新新新新旗新新旗新新旗新新旗旗新1992新新旗新新新旗新新旗新新旗新新旗新新旗新新新旗新新旗新新旗新新旗新新旗新新旗
返回true;
}
catch(Exception ex)
{
transaction.Rollback();
errorMessage =包含错误;
返回false;
}
}
}

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

  context.LoginUsers.Add(new LoginUsers()
{
名称=名称,
用户名=用户名,
密码= hashedPassword,
Created_At = DateTime.UtcNow,
Active = true
});

它抛出了以下异常:



< a href =https://i.stack.imgur.com/CGDgB.png =nofollow noreferrer>





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





我现在坚持在这一部分。搜索互联网和SO的解决方案,但都没有工作。任何人都面临这个问题,几乎没有任何帮助我检查可能的原因和如何解决它?



感谢大家谁能够帮助这个!

解决方案

我自己找到了解决方案。尝试和错误后,您的连接字符串中需要persistsecurityinfo = True才能使其正常工作。


I am currently working on a MVC project with Entity Framework 6 and MySQL database.

I have already install the following:

  • MySQL.Data v6.9.9
  • MySQL.Data.Entity v6.9.9
  • Entity Framework v6.1.3

Firstly, I added the connection string with the following in my web.config:-

<connectionStrings>
  <add name="MySQLContext" connectionString="server=localhost;database=db;uid=root;password=xxxxxxxx" providerName="MySql.Data.MySqlClient" />
</connectionStrings>

Then I added the following with the following:-

<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>

I have this context added in my model:

    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; }
    }

Then when I try to register/add a user (demo only):

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;
            }
        }
    }

All the models and connection string have gotten correctly. The connection string also works well with the traditional MySQLCommand. However, when the code reached this following part:

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

It threw following exception:

I have set the correct password for my connection string as well. I also added grant my privileges for my database with 'root'@'localhost'.

I now stuck at this part. Searching for solutions in internet and SO but none of them works. Anyone has faced this issue and few free to help me to check possible reason and how to fix it?

Thank you for everyone who kindly able to help this!

解决方案

I found out the solution by myself. After try and error, it turns out that you need "persistsecurityinfo=True" in your connection string to make it work.

这篇关于mysql 5.7与实体框架6 - 访问被拒绝用户'root'@'localhost'(使用密码:否)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆