簡體   English   中英

Visual Studio 2013 MVC MySql連接

[英]Visual Studio 2013 MVC MySql Connection

我正在嘗試將使用Visual Studio創建的MVC網站連接到MySQL。 使用SqlServer時,我的網站工作正常。

在我的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中使用用戶“ root”和密碼“ root”登錄沒有問題。

在我的DbContext類中,我添加了:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

當我運行整個程序時,我在global.asax中得到了nullreferenceexception:

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等已安裝...

您是否嘗試過其他連接字符串格式?

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

好的,我遇到了同樣的問題,並解決了以下問題:

MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(myConnectionstring);

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

然后使用tE1處理您的數據。...tE1也可以是包含所有類型化數據集的EF6上下文,依此類推。

重要的事情似乎是使用MySQL特定的連接覆蓋默認的DbContext連接。 使用MySQL連接字符串和默認的上下文連接是錯誤的,即使此“錯誤”在大多數情況下似乎都可以正常工作。

Jeltz

同時我解決了我的問題,我不得不從我的connectionString中排除'port = 3306'

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

暫無
暫無

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

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