连接字符串无法与MySQL

时间:2017-09-12 03:59:10

标签: mysql entity-framework

我正在REST API中使用MySQL DB创建visual studio 2015 in asp.net mvc 4.5。我已经完成了使用MySQL运行API所需的每一步,但我得到了这个例外。

  

{“Message”:“发生错误。”,“ExceptionMessage”:“初始化字符串的格式不符合从索引121开始的规范。”,“ExceptionType”:“ System.ArgumentException“,”StackTrace“:”在System.Data上的System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,Boolean useOdbcRules,String&amp; keyname,String&amp; keyvalue)\ r \ n。 Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable synonyms,Boolean firstKey)\ r \ n在System.Data.Common.DbConnectionOptions..ctor(String connectionString,Hashtable synonyms definitions,Boolean useOdbcRules)\ r \ n在MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(字符串值)的MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)\ r \ n的System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)\ r \ n )\ r \ n在System.Data.Entity.Infras tructure.Interception.DbConnectionDispatcher.b__18(DbConnection t,DbConnectionPropertyInterceptionContext 1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch [TTarget,TInterceptionContext](TTarget目标,Action 2 operation, TInterceptionContext interceptionContext, Action 3执行,Action 3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext 1 interceptionContext)\ r \在System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)\ r \ n在System.Data.Entity的System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name,AppConfig config)\ r \ n中.Internal.LazyInternalConnection.Initialize()\ r \ n在System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()\ r \ n,在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\ r \ n at System .Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\ r \ n at System.Data.Entity.Internal.Linq.InternalSet 1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet 1.GetEnumerator()\ r \ n at System.Data.Entity .Infrastructure.DbQuery 1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Collections.Generic.List 1..ctor(IEnumerable 1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable 1 sou rce)\ r \ n在D:\ Work \ DOT NET API \ RestWithMySQL \ RestWithMySQL \ Controllers \ ProductsController.cs中的RestWithMySQL.Controllers.ProductsController.Get():第19行“}

我认为我的连接字符串存在问题。我搜索过它但找不到确切的解决方案。

<connectionStrings>
    <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-RestWithMySQL-20170911031521.mdf;Initial Catalog=aspnet-RestWithMySQL-20170911031521;Integrated Security=True" providerName="System.Data.SqlClient" />-->
    <add name="ProductEntities" connectionString="metadata=res://*/ProductsModel.csdl|res://*/ProductsModel.ssdl|res://*/ProductsModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot; server=localhost;user id=root;database=accurate_dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="MySql.Data.MySqlClient"/>
    <!--<remove name="LocalMySqlServer" /><add name="LocalMySqlServer" connectionString="" providerName="MySql.Data.MySqlClient" />-->
</connectionStrings>

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:8)

将连接字符串剥离到最低限度。添加选项并查看导致其失败的原因。例如,integrated security=true不适用于MySQL。您需要添加password代替。

此处表格: ASP.NET MVC 4 EF5 with MySQL

尝试更接近这一点:

<add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost;port=3306;Initial Catalog=api_db;User Id=root;password=''"/>

答案 1 :(得分:1)

你的连接字符串最后不应该在其中有另一个引号吗?

<connectionStrings>
    <add name="ProductEntities" connectionString="metadata=res://*/ProductsModel.csdl|res://*/ProductsModel.ssdl|res://*/ProductsModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot; server=localhost;user id=root;database=accurate_dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

此外: 要使用MySQL提供程序,请考虑in this answer

提供的这些步骤

答案 2 :(得分:0)

你错过了一个结束&amp; quot;在连接字符串的末尾。

相关问题