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="server=localhost;user id=root;database=accurate_dev"" 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
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.
The first thing what I see that this exception details contain the exact problem you have:
"Keyword not supported" with
metadata
parameter clearly indicates that your connection string is used for Entity Framework purpose, marked withproviderName="System.Data.EntityClient"
(often used when EDMX file is present). What you need actually is theMySqlClient
provider connection string like this:The
MySqlClient
connection string can be obtained usingEntityConnection
:Then, use defined data context to perform query like this, inside
using
block mentioned above:If you want to use 2 connection strings instead (one for
MySqlClient
& other forEntityClient
), useMySqlClient
connection string forDbContext
class and use that class for LINQ to Entities query inCreateResponse
.Similar issue:
Create DataContext from Entity Framework connection string?