我正在研究連接到MySQL數據庫的ASP.NET Core MVC項目。我已經成功地在數據庫中創建了項目,但我在檢索數據時遇到了問題。在ASP.NET Core中從MySQL服務器獲取數據
我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核心,所以它可能是一個小事情,爲什麼這是行不通的。
如果我省略了html文件中的foreach語句,我不會收到任何錯誤,並且頁面呈現。
這是在控制檯被拋出的錯誤:
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware [0]已發生
未處理的異常:方法未找到:「無效Microsoft.EntityFrameworkCore。 Query.QueryContextFactory..ctor(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager,Microsoft.EntityFrameworkCore.Internal.IConcurrencyDetector,Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IChangeDetector)'。 System.MissingMethodException:Method not found:'Void 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提供商)
甲骨文莊嚴性交版本。 '7.0.6-IR31'用於EF Core 1.0。 '6.10.1-beta'是第一個支持EF Core 1的發佈版本。1(並於2017年發佈,而7.0.6-IR31是從2016年開始的),因此,您應該在EF Core 1.0中使用'6.10.1-beta' – Tseng
獲取「MySql.Data.EntityFrameworkCore」v6.10.1- beta是安裝「MySql.Data.EntityFrameworkCore.Design」c6.10.1-bate。 Nuget Package Manager找不到第一個軟件包,但它是第二個軟件包的依賴關係,並且會由其自動安裝。 –