连接字符串不能与MySQL一起使用

11 mysql entity-framework

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

{"Message":"发生错误.","ExceptionMessage":" 初始化字符串的格式不符合从索引121开始的规范. ","ExceptionType":"System.ArgumentException","StackTrace":"在System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,Boolean useOdbcRules,String&keyname,String&keyvalue)\ r \n在System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString, System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String)中的System.Data.Common.DbConnectionOptions..ctor(String connectionString,Hashtable synonyms,Boolean useOdbcRules)\ r \n的布尔buildChain,Hashtable peers,Boolean firstKey)\ r \n值)\ r \n在MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)\ r \n,位于System.Data.Entity的MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)\ r \n. Infrastructure.Interception.DbConnectionDispat cher.b__18(DbConnection t,DbConnectionPropertyInterceptionContext 1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch [TTarget,TInterceptionContext](TTarget target,Action 2 operation, TInterceptionContext interceptionContext, Action3 execution ,Action 3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext1 interceptionContext)\ r \n在System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)\ r \n \n位于System.Data.Entity的System.Data.Entity.LezyInternalConnection.TryInitializeFromAppConfig(String name,AppConfig config)\ r \n,位于System.Data.Entity的System.Data.Entity.Internal.LazyInternalConnection.Initialize()\ r \n.在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\ r \n at的System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\ r \n的Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()\ r \n System.Data.Entity.Internal.Linq.InternalSet 1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()\ r \n在System.Data.Entity.Infrastructure.DbQuery 1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Collections.Generic.List1..ctor(IEnumerable 1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\ r \n在RestWithMySQL.Controllers.ProductsController中.在D:\ Work\DOT NE中获取()T API\RestWithMySQL\RestWithMySQL\Controllers\ProductsController.cs:第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>
Run Code Online (Sandbox Code Playgroud)

任何帮助将受到高度赞赏.

小智 8

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

从这里形式: ASP.NET MVC 4 EF5与MySQL

尝试更接近这个:

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