2017-09-12 73 views
12

Tworzę REST API przy użyciu MySQL DB w visual studio 2015 in asp.net mvc 4.5. Zrobiłem każdy krok, który był potrzebny do uruchomienia API przy użyciu MySQL, ale otrzymuję ten wyjątek.Ciąg połączenia nie działa z MySQL

{ "message": "Wystąpił błąd.", "ExceptionMessage": "Format ciągu inicjowania jest niezgodny ze specyfikacją zaczynając od indeksu 121.", "ExceptionType": "System. ArgumentException "," StackTrace ":" w System.Data.Common.DbConnectionOptions.GetKeyValuePair (String connectionString, Int32 currentPosition, bufor StringBuilder, Boolean useOdbcRules, String & keyName) \ String \ r \ n w System.Data.Common. DbConnectionOptions.ParseInternal (Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonimy, Boolean firstKey) \ r \ n w System.Data.Common.DbConnectionOptions..ctor (String connectionString, Hashtable synonimy, Boolean useOdbcRules) \ r \ n at System .Dane .Common.DbConnectionStringBuilder.set_ConnectionString (wartość ciągu) \ r \ n w MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor (String connStr) \ r \ n at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString (wartość ciągu) \ r \ n w System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__18 (DbConnection t, DbConnectionPropertyInterceptionContext 1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch [TTarget, TInterceptionContext] (TTarget target, Akcja 2 operation, TInterceptionContext interceptionContext, Action 3 wykonanie, Akcja 3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext 1 interceptionContext) \ r \ n w System. Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting (ConnectionStringSettings appConfigConnection) \ r \ n w System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig (nazwa ciągu, konfiguracja AppConfig) \ r \ n w System.Data.Entity.Internal.LazyInternalConnection .Initialize() \ r \ n w System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel() \ r \ n w System.Data.Entity.Internal.LazyInternalContext.InitializeContext() \ r \ n w 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 w 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 źródło) \ r \ n w RestWithMySQL.Controllers.ProductsController.Get() w D: \ Work \ DOT NET API \ RestWithMySQL \ RestWithMySQL \ Controllers \ ProductsController.cs: line 19 "}

Myślę, że jest problem w moim ciągu połączenia. Szukałem go, ale nie mogłem znaleźć dokładnego rozwiązania.

<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> 

Każda pomoc będzie bardzo ceniona.

+1

Nie jestem pewien czy to jest problem, ale czy próbowałeś zmienić '"' na '"? –

Odpowiedz

7

Pozbądź się łańcucha połączenia do absolutnego minimum. Dodaj opcje i sprawdź, co powoduje, że się nie powiedzie. Na przykład integrated security=true nie będzie działał z MySQL. Musisz zamiast tego dodać password.

Forma tutaj: ASP.NET MVC 4 EF5 with MySQL

Spróbuj coś bliżej do tego:

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

+0

Zmieniłem ciąg połączenia, ale teraz otrzymuję to ' wygenerowany z pliku EDMX dla rozwoju bazy danych First lub Model First.To nie będzie działać poprawnie. Aby rozwiązać ten problem, nie należy usuwać linii kodu, która zgłasza ten wyjątek. " –

1

nie powinien ciąg połączenia mają inny cytat w nim w końcu?

<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> 

dalej: Aby korzystać dostawcy MySQL, należy rozważyć następujące kroki przewidziane in this answer

0

brakuje zamykającej & quot; na końcu ciągu połączenia.