我有一个关于如何在Azure网站中设置连接字符串以连接到MySQL数据库的问题。
我正在开发Azure Webistes中托管的ASP.NET MVC应用程序。它使用两个数据库:Azure本身的SQL Server和远程托管中的MySQL。
在Visual Studio中,我使用MySQL提供的.NET Connector生成模型,连接字符串如下所示:
<add name="myEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=myserver.com;user id=tom;password=tomspass;database=mydatabase"" providerName="System.Data.EntityClient" />
另一方面,我有一个脚本可以在Azure中为我的应用程序重新创建环境。它告诉Azure在Azure网站配置面板中设置两个连接字符串,从而覆盖web.config文件中的值。在脚本中我用这种方式定义它们:
$connectionStrings = ( `
@{Name = $sqlAppDatabaseName; Type = "SQLAzure"; ConnectionString = $sql.AppDatabase.ConnectionString}, `
@{Name = "myEntities"; Type = "MySql"; ConnectionString = "metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=myserver.com;user id=productionuser;password=productionpassword;database=mydatabase""}
)
不幸的是,它不起作用,当访问代码来访问MySQL中的数据时,会出现以下错误:
Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'System.Data.Entity.Core.EntityClient.EntityConnection'.
如果我在Azure面板中删除MySQL的连接字符串并使用web.config文件中定义的连接字符串(带有生产值),则不会出现错误!
我做错了什么?是不是在脚本中正确定义了连接?但是如果它与web.config文件相同呢?我发现这是因为在Azure中定义连接字符串时,&#39; providerName&#39;部分未设置,但为什么如果已经在web.config中设置了
非常感谢你的帮助!
答案 0 :(得分:0)
我没有尝试过这个,因为我没有方便的MySQL数据库,但我认为你有2个问题:
<add name="myEntities" connectionString="metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=myserver.com;user id=tom;password=tomspass;database=mydatabase"" providerName="MySql.Data.MySqlClient" />
并在您的Web.config中替换
<entityFramework>
<defaultConnectionFactory
type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider
invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
与
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"></remove>
<add name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.2.0"/>
</DbProviderFactories>
</system.data>
$connectionStrings = (
@{Name = $sqlAppDatabaseName; Type = "SQLAzure"; ConnectionString = $sql.AppDatabase.ConnectionString},
@ {Name =“myEntities”; Type =“MySql”; ProviderType =“MySql.Data.MySqlClient”; ConnectionString =“metadata = res:// /Models.MyModel.csdl|res:// /Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=MySql。 Data.MySqlClient; provider connection string =“server = myserver.com; user id = productionuser; password = productionpassword; database = mydatabase”“}
)