簡體   English   中英

從ASP.NET Core中的MySQL服務器獲取數據

[英]Get data from MySQL server in ASP.NET Core

我正在開發一個連接到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]

發生未處理的異常:找不到方法:'無效)”。 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)

設法解決它。

使用“ MySql.Data.EntityFrameworkCore”時出現此錯誤:“ 7.0.6-IR31”。 但是我的錯誤是,除了mysql nuget外,我還提取了“ Microsoft.EntityFrameworkCore.Relational”:“ 1.1.0”或“ Microsoft.EntityFrameworkCore”:“ 1.1.0”。 但是“ Microsoft.EntityFrameworkCore.Relational”:“ 1.1.0”是由mysql塊提取的,因此您要做的就是刪除“ Microsoft.EntityFrameworkCore.Relational”:“ 1.1.0”和/或“ Microsoft”。來自project.json的EntityFrameworkCore1.1.0”,它將使您的項目再次變得出色。

我認為您應該使用Pomelo或Sapient Guardian提供程序,而不是MySQL的此預發行版本。

https://docs.microsoft.com/en-us/ef/core/providers/sapient-guardian/

https://docs.microsoft.com/en-us/ef/core/providers/pomelo/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM