【发布时间】:2015-12-17 15:28:39
【问题描述】:
我正在尝试在我的 c# .Net 应用程序中连接到 MySQL 数据库
我在尝试连接时收到此错误:不支持关键字:“端口”。
该错误似乎表明我的连接字符串存在问题
<add name="mydataEntities" connectionString="server=myserver.com;port=3306;password=xxxx;user id=yyyy;database= mydatabase;persistsecurityinfo=True" providerName="MySql.Data.MySqlClient" />
我为以下内容添加了参考: MySql.Data 6.9.7.0(MySQL 的 ADO.Net 驱动程序) 和 MySql.Data.Entity.EF6 6.9.7.0(支持实体框架 6.0)
在我的配置中,我有:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider>
</providers>
</entityFramework>
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.9.7.0" newVersion="6.9.7.0" />
</dependentAssembly>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<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.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
连接字符串在这里设置:
public class Repository
{
protected mydataEntities DbContext;
public Repository()
{
DbContext = new mydataEntities (ConfigurationManager.ConnectionStrings["mydataEntities"].ConnectionString);
}
}
我注意到堆栈跟踪似乎正在使用 System.Data.SqlClient 提供程序,如下所示:
我已经删除了 MySql.Data 和 MySql.Data.Entity.EF6 引用,清理解决方案,添加回引用然后重建 .....
谁能帮我找出这个错误的来源?
为什么 MySQL 连接器不使用我的配置中指定的 providerName: MySql.Data.MySqlClient。
at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential)
at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
at System.Data.Entity.Infrastructure.SqlConnectionFactory.CreateConnection(String nameOrConnectionString)
at System.Data.Entity.Infrastructure.LocalDbConnectionFactory.CreateConnection(String nameOrConnectionString)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName()
at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName()
at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
【问题讨论】:
-
谁能帮我找出这个错误的根源?你在哪里指定
mydataEntities作为你的C#代码中使用的连接字符串? -
我更新了工单以显示我指定要使用的连接字符串的位置
-
对于它的价值,我遇到了
port关键字的类似问题。我也遇到了与allowuservariables关键字相同的问题。不过,您不需要使用port关键字,因为默认端口是 3306,看起来这就是您要连接的端口。 -
奇怪的是我有另一个使用关键字端口的应用程序并且它可以工作
-
我从连接字符串中删除了“端口”。现在出现此错误:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接):无法弄清楚为什么我的应用程序仍在使用:.Net SqlClient 数据提供者 - 我删除了所有引用