2016-07-14 10 views
0

現在、Entity Framework 6とMySQLデータベースを使用してMVCプロジェクトを行っています。エンティティフレームワーク6を使用したmysql 5.7 - ユーザー 'root' @ 'localhost'のアクセスが拒否されました(パスワードはNOを使用)

私はすでに次のようにインストールしている:、

  • MySQL.Data v6.9.9
  • MySQL.Data.Entityのv6.9.9
  • Entity Frameworkのはv6.1.3まず

を私はweb.configに次のような接続文字列を追加しました: -

<connectionStrings> 
    <add name="MySQLContext" connectionString="server=localhost;database=db;uid=root;password=xxxxxxxx" providerName="MySql.Data.MySqlClient" /> 
</connectionStrings> 
私が登録しようとすると、

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

は、次に/ユーザー(のみデモ)を追加します。私はこのコンテキストが私のモデルに追加されてい

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

: - :

は、それから私は、次のように次を追加しました

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

すべてのモデルと接続文字列が正しく取得されています。接続文字列は、従来のMySQLコマンドでもうまく動作します。コードは、この次の部分に達したときにしかし、私は同様に私の接続文字列の正しいパスワードを設定している

enter image description here

enter image description here

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

をこれは、次の例外をスローしました。私はまた、 'root' @ 'localhost'を使ってデータベースに私の特権を与えることを追加しました。

enter image description here

私は今、この部分で立ち往生。インターネットやSOのソリューションを探していますが、どれも機能していません。誰もがこの問題に直面しており、可能性のある理由を確認するのに手伝ってくれる人はほとんどいません。

親切にお手伝いできる皆様、ありがとうございます!

答えて

3

私は自分で解決策を見つけました。試行してエラーが発生した後、接続文字列に "persistsecurityinfo = True"が必要であることがわかります。

+0

私のために働いていません。 –

関連する問題