Web API不返回任何数据结果

时间:2017-09-26 09:56:41

标签: c# mysql rest visual-studio-2015 asp.net-web-api2

我使用WEB API数据库在visual studio 2015中创建了MySQL。现在我运行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();点,我添加了一个快速监视并获得以下异常

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

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

我看到的第一件事就是这个例外细节包含了你遇到的确切问题:

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

&#34;不支持关键字&#34; metadata参数清楚地表明您的连接字符串用于实体框架目的,标有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),请为MySqlClient类使用DbContext连接字符串并使用该类对于CreateResponse中的LINQ to Entities查询。

类似问题:

Create DataContext from Entity Framework connection string?