问题描述
我对如何设置的Azure网站的连接字符串来连接MySQL数据库的问题。
I have an issue on how to set the connection string in Azure Websites to connect to MySQL database.
我开发的Azure中的托管Webistes ASP.NET MVC应用程序。它使用两个数据库:SQL Server中的Azure的本身和MySQL在远程主机
I am developing ASP.NET MVC application hosted in Azure Webistes. It uses two databases: SQL Server in Azure itself and MySQL in a remote hosting.
在Visual Studio中我使用MySQL提供的.NET连接器来生成模型,连接字符串如下:
In Visual Studio I use the .NET Connector provided by MySQL to generate the model, and the connection string looks like this:
<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应用程序环境中的脚本。它告诉天青设置在超然在web.config文件中的值的Azure网站配置面板两个连接字符串。在脚本中,我将它们定义是这样的:
On the other, I have an script that recreates the environment for my application in Azure. It tells Azure to set two connections strings in Azure Websites configuration panel thus overriding the values in the web.config file. In the script I define them this way:
$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""}
)
不幸的是它不工作,达到code到MySQL中访问数据时,会出现以下错误:
Unfortunately it does not work, when hitting code to access data in MySQL the following error appears:
Unable to cast object of type 'MySql.Data.MySqlClient.MySqlConnection' to type 'System.Data.Entity.Core.EntityClient.EntityConnection'.
如果我删除的连接字符串为MySQL在Azure的面板,并使用在web.config文件(生产价值)定义的没有错误的作品!
If I remove the connection string for MySQL in Azure panel and use the one defined in the web.config file (with production values) there is no error works!
我做错了吗?是不是在脚本正确定义的连接?但如何,如果是相同的web.config文件的?我突然想起,这是因为在定义Azure中的连接字符串时的的providerName部分没有设置偏偏如果在web.config中已经设置??
What I am doing wrong? Is not the connection defined correctly in the script? But how if it is the same of the web.config file? It occurs to me this is because when defining the connection string in Azure the 'providerName' part of the is not set but why if it is already set in the web.config??
非常感谢你的帮助!
推荐答案
我没试过,因为我没有一个MySQL数据库方便,但我认为你有2个问题:
I haven't tried this as I don't have a MySQL database handy but I think you have 2 issues:
- 连接字符串的提供者名称部分是不对的,你应该把它定义为这样
&LT;添加名称=myEntities connectionString=\"metadata=res://*/Models.MyModel.csdl|res://*/Models.MyModel.ssdl|res://*/Models.MyModel.msl;provider=MySql.Data.MySqlClient;provider连接字符串=安培; QUOT;服务器= myserver.com;用户ID =汤姆密码= tomspass;数据库= MyDatabase的&放大器; QUOT;的providerName =MySql.Data.MySqlClient/&GT;
和在你的web.config替换
and in your Web.config replace
<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>
- 在脚本中,添加ProviderType属性
$ =是connectionStrings(
@ {名称= $ sqlAppDatabaseName; TYPE =SQLAzure;的ConnectionString = $ sql.AppDatabase.ConnectionString}
@ {名称=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连接字符串= QUOT;服务器= myserver.com;用户ID = productionuser;密码= productionpassword;数据库= MyDatabase的&QUOT;}
)
$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""} )
这篇关于如何定义在MySQL数据库的Azure网站的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!