[英]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.