ASP.Net Core 和 EFCore 的 CRUD 转换错误

ASP.Net Core and EFCore Conversion Errors with CRUD

我一直在尝试着手 ASP.NET Core 和 Entity Framework Core,最近为其创建了一个 Web 项目。我在此文件中收到 3 个错误,其中 2 个是相同的特定错误。

错误上方有注释。

错误 #1:

Cannot convert method group 'DeleteTransaction' to non-delegate type 'object'. Did you intend to invoke the method?

错误#2:

Cannot implicitly convert type 'string' to 'EFDataAccessLibrary.Models.DiscInfo'

代码:

@page "/DeleteEmployee/{CurrentID}"
@using EFDataAccessLibrary.Models;
@inject DiscService ObjDiscService
@inject NavigationManager NavigationManager

<h2>Delete Transaction</h2>
<hr />
<h3>Are you sure?</h3>

<div class="row">
    <div class="col-md-8">
        <div class="form-group">
            <label>Employee ID:</label>
            <label>@objDisc.Id</label>
        </div>
        <div class="form-group">
            <label>Category:</label>
            <label>@objDisc.Category</label>
        </div>
        <div class="form-group">
            <label>Description:</label>
            <label>@objDisc.Description</label>
        </div>
        <div class="form-group">
            <label>Amount:</label>
            <label>@objDisc.Amount</label>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        <div class="form-group">
                                                  // Error 1
            <button class="btn btn-danger" @onclick="@DeleteTransaction">Delete</button>
                                                  // Error 1
            <input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel" />
        </div>
    </div>
</div>

@code {
    [Parameter]
        public string CurrentID { get; set; }
        DiscInfo objDisc = new DiscInfo();

    protected override async Task OnInitializedAsync()
    {                                                                       // Error 2
        objDisc = await Task.Run(() => ObjDiscService.DeleteTransactionDisc((objDisc));
    }

    protected void DeleteTransaction()
    {
        ObjDiscService.DeleteTransactionDisc(objDisc);
        NavigationManager.NavigateTo("disc");
    }

    void Cancel()
    {
        NavigationManager.NavigateTo("disc");
    }

}

我已将该项目发布到 GitHub,如果有人想整体查看它,或者如果您认为另一个文件中存在问题导致此问题。

https://github.com/Ocheezyy/MoneyManagementCore

错误 1

我指的是 Build components,它有 <button class="btn btn-primary" @onclick="IncrementCount">Click me</button>。所以不需要为 DeleteTransactionCancel.

使用 @ 前缀

错误 2

您的 DeleteTransactionDisc 具有类型为 DccuInfo 的参数并且您正在传递对象 DiscInfo。这是你的方法。 public string DeleteTransactionDisc(DccuInfo objDisc).

将您的方法参数类型更新为DiscInfo,它不会显示任何错误。

public string DeleteTransactionDisc(DiscInfo objDisc)

而且我在你的DccuService.cs中看到了。在构造函数中,它如下所示,但你应该有 _db = db;

private readonly AppDbContext _db;
public DccuService(AppDbContext db)
{
    db = _db;
}

正确代码

private readonly AppDbContext _db;
public DccuService(AppDbContext db)
{
    _db = db;
}