Web API not returning any data result

2019-08-28 00:15发布

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

Controller

 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.

Update 1

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

enter image description here

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

Any help would be highly appreciated.

1条回答
疯言疯语
2楼-- · 2019-08-28 00:44

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"

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

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

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());

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.

Similar issue:

Create DataContext from Entity Framework connection string?

查看更多
登录 后发表回答