2017-09-26 9 views
0

WEB APIを作成しました。MySQL DBを使用しました。今私はAPIを実行します。私は{"Message":"No Data found"}例外を得ています。以下は私のコードはWeb APIからデータが返されない

<add name="MeterEntities" connectionString="metadata=res://*/Models.MeterModel.csdl|res://*/Models.MeterModel.ssdl|res://*/Models.MeterModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;database=accurate_dev&quot;" providerName="System.Data.EntityClient" /> 

のWeb.config

コントローラ

public class MetersInfoController : ApiController 
{ 
    public MeterEntities mEntities = new MeterEntities(); 


    public HttpResponseMessage Get() 
    { 
     try 
     { 
      return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.ToList()); 
     } 
     catch (Exception) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found"); 
     } 
    } 

    // GET by ID 
    public HttpResponseMessage Get(int id) 
    { 
     try 
     { 
      return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.SingleOrDefault(m => m.id == id), Environment.NewLine); 
     } 
     catch 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found"); 
     } 
    } 

    // GET by serial number 
    public HttpResponseMessage Get(string msn) 
    { 
     try 
     { 
      return Request.CreateResponse(HttpStatusCode.Found, mEntities.meters_info_dev.SingleOrDefault(m => m.meter_msn == msn), Environment.NewLine); 
     } 
     catch 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found"); 
     } 
    } 

である私は、問題が何であるかを知るためにしようとしたが、それを見つけることができませんでした。私は、接続文字列に問題があるかもしれないと思うが、私はそれでは分かりません。

アップデート1

コードをデバッグした後、ポイントpublic MeterEntities mEntities = new MeterEntities();で私は

enter image description here

はまた、私は本当の例外をプリントアウトしようとした以下の例外を迅速に時計を追加しましたメッセージ。

{"Message":"An error has occurred.","ExceptionMessage":"Keyword not supported.\r\nParameter name: metadata","ExceptionType":"System.ArgumentException","StackTrace":" at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.GetOption(String key)\r\n at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item(String keyword, Object value)\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.<SetConnectionString>b__18(DbConnection t, DbConnectionPropertyInterceptionContext`1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 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.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 source)\r\n at WebServiceMySQL.Controllers.MetersInfoController.Get() in E:\\Work\\NEW API\\WebServiceMySQL\\WebServiceMySQL\\Controllers\\MetersInfoController.cs:line 22"} 

ご協力いただければ幸いです。

+0

あなたは、接続文字列についてよろしいですか? –

+0

データベースに接続できない可能性があります。デバッグを試しましたか? –

+0

EF接続文字列を表示していますが、使用していますか?データベースが接続されているかどうかわからない場合は、標準の 'SqlClient'接続文字列を使用してみてください。 –

答えて

1

私は、この例外の詳細は、あなたが持っている正確な問題が含まれていることが何を参照してください最初のもの:はっきりmetadataパラメータでを「キーワードはサポートされていません」

"ExceptionMessage":"Keyword not supported.\r\nParameter name: metadata","ExceptionType":"System.ArgumentException" 

あなたの接続文字列は、Entity Frameworkの目的のために使用されていることを示していますproviderName="System.Data.EntityClient"とマークされています(EDMXファイルが存在する場合によく使用されます)。あなたが実際に必要なのは、このようなMySqlClientプロバイダの接続文字列です:

<add name="MeterConnection" connectionString="server=localhost;user id=root;database=accurate_dev" providerName="MySql.Data.MySqlClient" /> 

MySqlClient接続文字列がEntityConnectionを使用して取得することができます。

using (EntityConnection efConnection = new EntityConnection("[EF_connection_string]")) 
{ 

    // DataContext is your data context class name inherited from DbContext 
    DataContext dataContext = new DataContext(efConnection); 
} 

その後、内部の、このようなクエリを実行するために定義されたデータコンテキストを使用しますusingブロック:

return Request.CreateResponse(HttpStatusCode.Found, dataContext.meters_info_dev.ToList()); 

2つの接続を使用する場合代わりにMySqlClient &、EntityClientの場合はクラスの接続文字列をMySqlClientとし、そのクラスをLINQ to EntitiesクエリのCreateResponseに使用してください。

同様の問題:

Create DataContext from Entity Framework connection string?

関連する問題