2014-03-02 107 views
1

我試圖將我用Visual Studio創建的mvc網站連接到MySQL。 使用SqlServer時,我的網站工作得很好。Visual Studio 2013 MVC MySql連接

在我的web.config我說:

<connectionStrings> 
    <add name="MySqlConnection" connectionString ="Server=localhost;port=3306;Database=BachelorParty;Uid=root;Pwd=root;" providerName="MySql.Data.MySqlClient" /> 
</connectionStrings> 
<entityFramework> 
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
<providers> 
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" /> 
</providers> 

沒有與用戶 '根' 和密碼 '根' 在MySQL Workbench中登錄沒有問題。

在我的DbContext類我說:

[DbConfigurationType(typeof(MySqlEFConfiguration))] 

當我運行了整個事情,我得到一個NullReferenceException在Global.asax中:

Database.SetInitializer(new BachelorPartyInitializer()); 
new BachelorPartyContext().Students.ToList(); //<--nullreferenceexception 

在刷新服務器資源管理器中會發生以下情況:

Authentication to host 'localhost' for user 'root' using method 'mysql_native_password' failed with message: 
Access denied for user 'root'@'localhost' (using password: NO) 

關於修復這個道德敗壞問題的任何想法? EntityFramework,MySql.Data,MySql.Data.Entities,MySql Connector等安裝...

回答

0

同時我固定我的問題,我不得不從我的connectionString

<connectionStrings> 
<add name="BachelorProef" connectionString="Server=localhost;Database=BachelorParty;Uid=root;Pwd=root;" providerName="MySql.Data.MySqlClient" /> 
</connectionStrings> 
0

您是否嘗試過不同的連接字符串格式?

<add name="MySqlConnection" 
    connectionString="server=localhost;User Id=root;password=root;Persist Security Info=True;database=BachelorParty;port=3306;" 
    providerName="MySql.Data.MySqlClient" /> 
0

確定我有這個同樣的問題,解決如下:

MySql.Data.MySqlClient.MySqlConnection MSC =新MySql.Data.MySqlClient.MySqlConnection(myConnectionstring);

System.Data.Entity.DbContext tE1 = new System.Data.Entity.DbContext(msc,false);

然後用TE1與您的數據工作.... TE1也可以與您的所有類型化數據集的EF6背景等等...........

重要的事情似乎要重寫與MySQL特定連接的默認DbContext連接。在默認的上下文連接中使用MySQL連接字符串是一個錯誤,儘管這個「錯誤」在大多數情況下似乎可以正常工作。

Jeltz

+0

排除 '端口= 3306' 你是100%肯定,你的ConnectionString是確定的?參看我的回答是,我不得不排除'port = 3306' – Seneca