簡體   English   中英

帶有實體框架6的mysql 5.7-拒絕用戶'root'@'localhost'的訪問(使用密碼:NO)

[英]mysql 5.7 with entity framework 6 - Access denied for user 'root'@'localhost' (using password: NO)

我目前正在使用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>

然后我添加了以下內容:

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

我在模型中添加了以下上下文:

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

然后,當我嘗試注冊/添加用戶時(僅演示):

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

所有模型和連接字符串均已正確獲得。 連接字符串也可以與傳統的MySQLCommand一起很好地工作。 但是,當代碼到達以下部分時:

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

它引發了以下異常:

在此處輸入圖片說明

在此處輸入圖片說明

我也為我的連接字符串設置了正確的密碼。 我還添加了使用“ root” @“ localhost”授予我的數據庫特權。

在此處輸入圖片說明

我現在停留在這一部分。 在Internet和SO中搜索解決方案,但沒有一個起作用。 任何人都面對過這個問題,很少有人有空幫助我檢查可能的原因以及如何解決?

感謝所有能幫助您的人!

我自己找到了解決方案。 經過嘗試和錯誤之后,事實證明您需要在連接字符串中使用“ persistsecurityinfo = True”才能使其正常運行。

我有一個與此非常相似的問題。 對我來說,問題出在我的數據庫視圖上,特別是視圖的定義器與我的連接字符串中的內容不匹配。 在我的dbcontext中,我有一些從視圖映射到的對象。

當我查看視圖的定義時,我看到類似

DEFINER = `admin`@`%` 

但是我的連接字符串是

connectionString="server=localhost;user id=myUserId;Password=*****;database=mydatabase; Allow User Variables=True"

因此,解決方案是將DEFINER = admin @ %替換為DEFINER = myUserId @ localhost

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM