【发布时间】:2017-02-26 10:13:31
【问题描述】:
我正在开发一个连接到 MySQL 数据库的 ASP.NET Core MVC 项目。我已经成功地在数据库中创建了项目,但是我在检索数据时遇到了问题。
我的 startup.cs 如下所示:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddScoped<ISqlService, SqlService>();
services.AddDbContext<WebAPIDataContext>(options => options.UseMySQL(Configuration.GetConnectionString("MySqlServer")));
}
我用于检索数据的控制器如下所示:
public IActionResult Index()
{
var model = new ItemsViewModel();
model.items = _sqlService.GetAllItems();
return View(model);
}
我的服务 (EF) 如下所示:
public IEnumerable<Item> GetAllItems()
{
return _webAPIDataContext.items;
}
我的 html 看起来像这样:
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Headline</th>
<th>Category</th>
<th>Description</th>
<th>Picture URL</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model.items) {
<tr>
<td>@item.items_id</td>
<td>@item.items_headline</td>
<td>@item.items_category</td>
<td>@item.items_description</td>
<td>@item.items_picture</td>
<td>
<div class="pull-right">
<a href="/items/edit/@item.items_id" class="btn btn-default">Edit</a>
<a href="/items/remove/@item.items_id" class="btn btn-danger">Delete</a>
</div>
</td>
</tr>
}
</table>
我是 C# / ASP.NET Core 的新手,所以这可能是一件小事。
如果我在 html 文件中省略了 foreach 语句,我不会收到任何错误,并且页面会呈现。
这是控制台中抛出的错误:
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
发生未处理的异常:找不到方法:'Void Microsoft.EntityFrameworkCore.Query.QueryContextFactory..ctor(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager, Microsoft.EntityFrameworkCore.Internal.IConcurrencyDetector, Microsoft.EntityFrameworkCore.ChangeTracking。内部.IChangeDetector)'。 System.MissingMethodException:找不到方法:'无效 Microsoft.EntityFrameworkCore.Query.QueryContextFactory..ctor(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager,Microsoft.EntityFrameworkCore.Internal.IConcurrencyDetector,Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IChangeDetector)' .
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
【问题讨论】:
标签: c# mysql asp.net-mvc entity-framework asp.net-core