gpt4 book ai didi

c# - Web API 未返回任何数据结果

转载 作者:行者123 更新时间:2023-11-29 10:35:58 26 4
gpt4 key购买 nike

我使用 MySQL DB 在 Visual Studio 2015 中创建了一个 WEB API。现在当我运行 API 时。我收到 {"Message":"No Data found"} 异常。下面是我的代码

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

我试图找出问题所在,但无法找到它。我认为连接字符串可能有问题,但我不确定。

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

任何帮助将不胜感激。

最佳答案

我看到的第一件事是这个异常详细信息包含您遇到的确切问题:

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

MySqlClient 连接字符串可以使用 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 block 内使用定义的数据上下文执行查询:

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?

关于c# - Web API 未返回任何数据结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com