连接字符串无法与MySQL配合使用。

11
我正在使用MySQL DBvisual studio 2015 in asp.net mvc 4.5中创建一个REST API。我已经按照需要的每个步骤运行了API,但是出现了这个异常。

{"Message":"发生错误。","ExceptionMessage":"初始化字符串的格式不符合从索引121开始的规范。","ExceptionType":"System.ArgumentException","StackTrace":" at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)\r\n at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)\r\n at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)\r\n at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)\r\n at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)\r\n at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__18(DbConnection t, DbConnectionPropertyInterceptionContext1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action2 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext1 interceptionContext)\r\n at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)\r\n at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)\r\n at System.Data.Entity.Internal.LazyInternalConnection.Initialize()\r\n at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()\r\n at 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.InternalSet1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()\r\n at System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Collections.Generic.List1..ctor(IEnumerable1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\r\n at RestWithMySQL.Controllers.ProductsController.Get() in D:\Work\DOT NET API\RestWithMySQL\RestWithMySQL\Controllers\ProductsController.cs:line 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>

非常感谢您的帮助。


1
不确定这是否是问题,但您尝试将&quot;更改为"了吗? - Steve Chambers
3个回答

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=''"/>

我已经更改了连接字符串,但现在出现了这个错误:“上下文正在使用从EDMX文件生成的代码进行Code First模式,用于Database First或Model First开发。这将无法正常工作。要解决此问题,请不要删除引发此异常的代码行。” - user6789930

2
您的连接字符串末尾应该再加一个引号吧?
<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提供程序,请考虑采取这些步骤 在这个答案中提供

0

你的连接字符串末尾缺少一个闭合的 &quot;。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接