I'm working on a ASP.NET Core MVC Project which connects to a MySQL database. I've successfully been able to create items in the database, but I'm having trouble retrieving data.
My startup.cs looks like this:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddScoped<ISqlService, SqlService>();
services.AddDbContext<WebAPIDataContext>(options => options.UseMySQL(Configuration.GetConnectionString("MySqlServer")));
}
My controller for retrieving data looks like this:
public IActionResult Index()
{
var model = new ItemsViewModel();
model.items = _sqlService.GetAllItems();
return View(model);
}
My Service (EF) looks like this:
public IEnumerable<Item> GetAllItems()
{
return _webAPIDataContext.items;
}
My html looks like this:
<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>
I'm pretty new in C# / ASP.NET Core so it might be a small thing why this is not working.
If I omit the foreach statement in the html file I don't get any errors, and the page renders.
This is the error being thrown in the console:
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
An unhandled exception has occurred: Method not found: 'Void 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)'.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
Managed to solve it.
I got this error while using the "MySql.Data.EntityFrameworkCore": "7.0.6-IR31". But my error was that i was pulling in the "Microsoft.EntityFrameworkCore.Relational":"1.1.0" or "Microsoft.EntityFrameworkCore":"1.1.0" in addition to the mysql nuget. But the "Microsoft.EntityFrameworkCore.Relational":"1.1.0" is pulled by the mysql nugget so all you have to do is to remove the "Microsoft.EntityFrameworkCore.Relational":"1.1.0" and/or "Microsoft.EntityFrameworkCore1.1.0" from your project.json and it will make your project great again.
I think you should use the Pomelo or the Sapient Guardian provider rather than this pre release version from MySQL.
https://docs.microsoft.com/en-us/ef/core/providers/sapient-guardian/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.