Web API不返回任何数据结果 [英] Web API not returning any data result

查看:217
本文介绍了Web API不返回任何数据结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用MySQL DB在visual studio 2015中创建了WEB API.现在,当我运行API时.我收到{"Message":"No Data found"}异常.下面是我的代码

I have created a WEB API in visual studio 2015 using MySQL DB. Now when I run the API. I am getting {"Message":"No Data found"} exception. Below is my code

控制器

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

我试图知道问题出在哪里,但是找不到.我认为连接字符串可能存在问题,但是我不确定.

I have tried to know what is the problem but couldn't able to find it. I think there might be an issue with the connection string but i am not sure with that.

更新1

在调试代码后,在public MeterEntities mEntities = new MeterEntities();点,我添加了一个快速监视并获得了以下异常

After debugging the code, and at point public MeterEntities mEntities = new MeterEntities(); I add a quick watch and got the below exceptions

我还尝试打印出真正的异常消息.

Also I have tried to print out the real exception message.

{"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"}

任何帮助将不胜感激.

推荐答案

我首先看到的是此异常详细信息包含您所遇到的确切问题:

The first thing what I see that this exception details contain the exact problem you have:

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

带有metadata参数的

不支持关键字" 清楚地表明您的连接字符串用于Entity Framework,标有providerName="System.Data.EntityClient"(通常在存在EDMX文件时使用).您实际上需要的是MySqlClient提供程序连接字符串,如下所示:

"Keyword not supported" with metadata parameter clearly indicates that your connection string is used for Entity Framework purpose, marked with providerName="System.Data.EntityClient" (often used when EDMX file is present). What you need actually is the MySqlClient provider connection string like this:

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

MySqlClient连接字符串可以使用

The MySqlClient connection string can be obtained using 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块中,使用定义的数据上下文执行如下查询:

Then, use defined data context to perform query like this, inside using block mentioned above:

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

如果要改为使用2个连接字符串(对于MySqlClient使用一个连接字符串,对于EntityClient使用其他连接字符串),请对DbContext类使用MySqlClient连接字符串,并对.

If you want to use 2 connection strings instead (one for MySqlClient & other for EntityClient), use MySqlClient connection string for DbContext class and use that class for LINQ to Entities query in CreateResponse.

类似的问题:

从Entity Framework连接字符串创建DataContext吗?

这篇关于Web API不返回任何数据结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆