簡體   English   中英

Web API不返回任何數據結果

[英]Web API not returning any data result

我已經在visual studio 2015使用MySQL DB創建了WEB API 現在,當我運行API時。 我收到{"Message":"No Data found"}異常。 下面是我的代碼

調節器

 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");
        }
    }

Web.config文件

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

我試圖知道問題出在哪里,但是找不到。 我認為連接字符串可能存在問題,但是我不確定。

更新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"}

任何幫助將不勝感激。

我首先看到的是此異常詳細信息包含您所遇到的確切問題:

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

帶有metadata參數的“不支持關鍵字”清楚地表明您的連接字符串用於Entity Framework,標有providerName="System.Data.EntityClient" (當存在EDMX文件時通常使用)。 您實際上需要的是MySqlClient提供程序連接字符串,如下所示:

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

可以使用EntityConnection獲得MySqlClient連接字符串:

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 ), DbContext MySqlClient連接字符串用於DbContext類,並將該類用於LINQ to Entities查詢(在CreateResponse

類似問題:

從實體框架連接字符串創建DataContext?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM