【问题标题】:ASP.Net Core and EFCore Conversion Errors with CRUD使用 CRUD 的 ASP.Net Core 和 EFCore 转换错误
【发布时间】:2020-06-05 13:24:15
【问题描述】:

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

错误上面标有注释。

错误 #1:

无法将方法组“DeleteTransaction”转换为非委托类型“对象”。您是否打算调用该方法?

错误 #2:

无法将类型“字符串”隐式转换为“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

【问题讨论】:

标签: c# asp.net-core entity-framework-core crud


【解决方案1】:

错误 1

我参考了Build components,它有&lt;button class="btn btn-primary" @onclick="IncrementCount"&gt;Click me&lt;/button&gt;。所以不需要为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;
}

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 2020-07-11
  • 2022-12-14
  • 2021-11-09
  • 2021-10-20
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
相关资源
最近更新 更多