简体   繁体   English

Visual Studio 2013 MVC MySql连接

[英]Visual Studio 2013 MVC MySql Connection

I'm trying to connect an mvc website I created with Visual Studio to MySQL. 我正在尝试将使用Visual Studio创建的MVC网站连接到MySQL。 My website worked just fine when using SqlServer. 使用SqlServer时,我的网站工作正常。

In my Web.config I added: 在我的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>

There are no problems logging in with user 'root' and password 'root' in MySQL Workbench. 在MySQL Workbench中使用用户“ root”和密码“ root”登录没有问题。

In my DbContext class I added: 在我的DbContext类中,我添加了:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

When I run the whole thing I get a nullreferenceexception in global.asax: 当我运行整个程序时,我在global.asax中得到了nullreferenceexception:

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

On refreshing the server explorer the following happens: 刷新服务器资源管理器时,会发生以下情况:

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

Any ideas on fixing this demoralizing issue? 关于解决这个令人沮丧的问题有什么想法吗? EntityFramework, MySql.Data, MySql.Data.Entities, MySql Connector etc. are installed... EntityFramework,MySql.Data,MySql.Data.Entities,MySql Connector等已安装...

Have you tried different connection string format? 您是否尝试过其他连接字符串格式?

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

OK I had this same problem and solved as follows: 好的,我遇到了同样的问题,并解决了以下问题:

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

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

Then use tE1 to work with your data....tE1 can also be an EF6 context with all your typed datasets and so on........... 然后使用tE1处理您的数据。...tE1也可以是包含所有类型化数据集的EF6上下文,依此类推。

The important thing seems to be to override the default DbContext connection with a MySQL specific connection. 重要的事情似乎是使用MySQL特定的连接覆盖默认的DbContext连接。 It is a mistake to use an MySQL connection string with the default context connection, even though this "mistake" seems to work fine for most purposes. 使用MySQL连接字符串和默认的上下文连接是错误的,即使此“错误”在大多数情况下似乎都可以正常工作。

Jeltz Jeltz

Meanwhile I fixed my problem, I had to exclude 'port=3306' from my connectionString 同时我解决了我的问题,我不得不从我的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