に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="server=localhost;user id=root;database=accurate_dev"" 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();
で私は
はまた、私は本当の例外をプリントアウトしようとした以下の例外を迅速に時計を追加しましたメッセージ。
{"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"}
ご協力いただければ幸いです。
あなたは、接続文字列についてよろしいですか? –
データベースに接続できない可能性があります。デバッグを試しましたか? –
EF接続文字列を表示していますが、使用していますか?データベースが接続されているかどうかわからない場合は、標準の 'SqlClient'接続文字列を使用してみてください。 –